. NET file Upload control using the method to modify Web.config file upload large file _ practical skills

Source: Internet
Author: User
Tags file upload server memory

To modify the Webcong file:

The following are the referenced contents:

Copy Code code as follows:

<system.web>
Usefullyqualifiedredirecturl= "true"
executiontimeout= "6000"
Usefullyqualifiedredirecturl= "false"
Minfreethreads= "8"
Minlocalrequestfreethreads= "4"
apprequestqueuelimit= "100"
Enableversionheader= "true"
/>
</system.web>

Which is closely related to the upload is:

Copy Code code as follows:

maxRequestLength

Indicates the maximum file upload size supported by asp.net.

This restriction can be used to prevent denial of service attacks caused by a user passing a large number of files to the server.

The specified size is in kilobytes.

The default value is 4096 KB (4 MB).

Copy Code code as follows:

Executiontimeout

Indicates the maximum number of seconds allowed to execute a request before it is automatically closed by asp.net.

The unit is seconds, and when uploading large files, set this up a bit larger.

If the server memory is 512M, you can upload files of size 160M. (haven't tried, csdn the unanimous opinion of the public posts.) )

The setting of the web.config here is over.

However, once the size of the uploaded file exceeds the file size of this setting, the following error occurs: The page cannot display that the page you are viewing is currently unavailable. Your web site may be experiencing technical problems, or you need to adjust your browser settings.

Although cannot solve, that also must catch this mistake! What do we do? Because this error is a foreground error caused by the file control, it is not feasible to use Try...catch in the background to capture it.

So I thought of using. NET error capture page mechanism to deal with. Yes, it works.

The following are the referenced contents:
1, first set web.config

Copy Code code as follows:

<customerrors mode= "on"/>

2, create a new error.aspx file, specifically to catch the wrong.

3. Add page directives to the front page of the ASPX page of the uploaded file. Errorpage= "Uploaderror.aspx"

4. Add some code in the error.aspx to determine if the error message is a foreground error caused by file.

Copy Code code as follows:

public class UploadError:System.Web.UI.Page
{
private void Page_Load (object sender, System.EventArgs e)
{
Exception ex = Server.GetLastError ();
if (ex!= null)
{
Response.Redirect (".. /error.aspx ");
}
else//Foreground error ex is null
{
Response.Redirect ("Uploadexcel.aspx?err=1"); Re-jump to upload page,
The Err parameter is added to display an error message
}
}

5, display error prompts.

Copy Code code as follows:

public class Uploadexcel:System.Web.UI.Page
{
private void Page_Load (object sender, System.EventArgs e)
{
if (request["err"] = = "1")
{
Page.registerstartupscript ("Budget", "<script language = javascript>
Alert (' Upload file has failed! File size is too large! ') </script> ");
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.