Upload large files using ASP. NET

Source: Internet
Author: User

We encountered one or more problems when uploading large files. Setting a large value of maxrequestlength 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. You can refer to this post to study the mechanism of this error.

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 ())
{
Int n = 1024;
Byte [] bs2 = new byte [N];
While (WR. readentitybody (bs2, n)> 0)
{
.....
}
}

Chris Hynes provides us with such a solution (using httpmodule) that allows you to upload large files and display the upload progress in Real Time:

ASP. NET upload magic part 2

Here is the pptfile of his lecture:

Uploading with ASP. NET (Part 1)

Uploading with ASP. NET (Part 2)

Related Article

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.