Upload a file using the. NET file control

Source: Internet
Author: User
Tags server memory

When using the. NET file control to upload a file sh, You need to modify the webcong file. Otherwise, the file cannot be uploaded when it is large.

The modification method is as follows ::
<System. Web>
& Lt; httpruntime maxrequestlength = "40690"
Usefullyqualifiedredirecturl = "true"
Executiontimeout = "6000"
Usefullyqualifiedredirecturl = "false"
Minfreethreads = "8"
Minlocalrequestfreethreads = "4"
Apprequestqueuelimit = "100"
Enableversionheader = "true"
/>
</System. Web>

Which is closely related to upload:
Maxrequestlength
Indicates the maximum file upload size supported by ASP. NET.
This restriction can be used to prevent DoS attacks caused by a large number of files being transferred to the server.
The specified size is in KB.
The default value is 4096 KB (4 MB ).

Executiontimeout
Indicates the maximum number of seconds allowed to execute a request before it is automatically disabled by ASP. NET.
The Unit is seconds. When uploading a large file, this setting is larger.

If the server memory is 512 MB, files with a size of MB can be uploaded. (I have never tried it. Comments from all the posts on csdn .)

The Web. config settings are now complete.
However, once the size of the uploaded file exceeds the configured file size range, the following error occurs:
This page cannot be displayed
The page you want to view is currently unavailable. The website may encounter technical problems, or you need to adjust the browser settings.

If this problem cannot be solved, capture the error! What should we do?
I have eaten a few fish recently and thought about it. Because this error is caused by a file control foreground error, it is not feasible to use try... catch in the background.
So I thought of using the. NET error capture page mechanism for processing.

1. Set web. config first
<Customerrors mode = "on"/>
2. Create an error. aspx file to capture errors.
3. Add the page command on the front-end page of The ASPX page of the uploaded file. Errorpage = "uploaderror. aspx"
4. Add some Code To determine whether the error message is a file-caused foreground error.
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 "); // redirect to the upload page again, and add the err parameter to display the error message.
}
}

5. An error message is displayed.
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.