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)