Solve the problem that Asp.net cannot upload large files

Source: Internet
Author: User
For Asp.net, only 2 MB files can be uploaded by default. The following configuration is added in Web. config. You can customize the maximum file size.

<Httpruntime

Executiontimeout = "300"

Maxrequestlength = "40960"

Usefullyqualifiedredirecturl = "false"/>
Sometimes this setting does not solve the problem. We have encountered one or more problems when uploading large files. Setting a large maxrequestlength value does not completely solve the problem, because Asp.net blocks the entire file until it is loaded into the memory and then processes it. In fact, if the file is large, we often see that Internet Explorer displays "the page cannot be displayed-cannot find server or DNS error". It seems that this error cannot be caught. Why? Because this is a client side error, application_error on the server side cannot be handled. If the following error occurs:
Handling server error when Upload File too large

The solution is to use the implicit httpworkerrequest and its getpreloadedentitybody and readentitybody methods to read data from the pipe created by IIS for Asp.net in blocks.

iserviceprovider provider = (iserviceprovider) httpcontext. current;
httpworkerrequest wR = (httpworkerrequest) Provider. getservice (typeof (httpworkerrequest);
byte [] BS = Wr. getpreloadedentitybody ();
....
If (! Wr. isentireentitybodyispreloaded ()
{< br> int n = 1024;
byte [] bs2 = new byte [N];
while (WR. readentitybody (bs2, n)> 0)
{< br> .....
}< BR >}

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.