How to upload large files using the. NET file control

Source: Internet
Author: User
Select blog from qdzx2008

After traversing all the posts about uploading large files in csdn, I wrote this spam. (:-))

There are several ways to upload large files:

1. The thought httpworkerrequest method is too difficult to understand :-(
2. Use the third-party control aspnetupload for money !! Forget it. We still like free.
3. Modify the Web. config file, but the error cannot be captured.
4. Upload through FTP. The server must provide the FTP service.

No. You have to choose the third method. Who made me stupid! (It's too stupid. It doesn't matter. If you want to eat more fish, I heard that fish can be smarter .)

So study the third method carefully!

Modify the webcong file:

<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. Feasible.

1. Set web. config <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 in error. aspx to determine whether the error message is a foreground error caused by file. 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 "); // jump to the upload page again, and add the err parameter to display the error message} 5. display the error message. 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> ") ;}} 6. Close the job. (This fish is not eaten in vain.) After the above modification, the current upload is changed to the file size specified by the program. If the file size exceeds, an error message is displayed. In this way, it is much better. At least I can accept it myself, and it is not difficult to explain it to users.

 

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.