How to upload files using the. NET file control

Source: Internet
Author: User

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>
& 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 the 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, the size is larger.
If the server memory is 512 MB, you can upload files of MB. (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.
this error should be captured even if it cannot be solved! What should we do?
I have eaten a few fish recently and thought about it. This error is a front-end error caused by the file control, so I want to use try in the background... catch won't work.
I thought of using the. NET error capture page mechanism for processing. Feasible.

1. Set web. config

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. in error. add Code to aspx to determine whether the error message is a foreground error caused by file.
public class uploaderror: system. web. UI. page
{< br> private void page_load (Object sender, system. eventargs e)
{< br> exception EX = server. getlasterror ();
If (ex! = NULL)
{< br> response. redirect (".. /error. aspx ");
}< br> else // foreground error ex is null
{< br> response. redirect ("uploadexcel. aspx? Err = 1 "); // redirect to the upload page again.
the err parameter is added to display the error message
}< BR >}

5. An error message is displayed.
public class uploadexcel: system. web. UI. page
{< br> private void page_load (Object sender, system. eventargs e)
{< br> If (request ["Err"] = "1")
{< br> page. registerstartupscript ("budget", " ");
}< BR >}< br> 6. Close the ticket. (This fish is not white)
after the above modification, the current upload is changed to Program to specify the file size. If the file size exceeds, an error message is displayed.
in this way, it will be much better. At least I can accept it myself, and it will not be 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.