Recently used in a projectHTTPThe file upload function is implemented by anyone who has done similar functions.HTTPThe file upload function is not difficult to use.ASP. NETIt is more convenient, and the server-side controlsHtmlinputfileProvides powerful support. Everything went smoothly, and the function was quickly realized. The word "beautiful fields" appears periodically in the TV series: Everything looks beautiful ".
However, when trying to upload a large file, everything seems ugly. - ProgramCrash, and" The page cannot be displayed "Error page. After checking the relevant information, you will be informed ASP. NET The maximum value of the default file to be uploaded is 4 m , In Web. config Can be set, but even if it is set to be larger, the size of the files uploaded by the user may exceed the limit. So I thought about Global. asax In Application_error Capture" Maximum request length exceeded. "Exception then make the page Redirect To Custom error page . However, this method does not work. Application_error " Maximum request length exceeded. "Exception, but the page cannot Redirect To Custom error page , Still" The page cannot be displayed ".
Things become tricky, and many technologiesArticleAlso, this is HTTP Protocol to guarantee Web Server stability and security restrictions. Even the official Microsoft Support It also says:" WhenMaxrequestlengthAttribute is set in the machine. config file and then a request is posted (for example, a file upload) that exceeds the valueMaxrequestlength, A custom error page cannot be displayed. Instead, Microsoft Internet Explorer will display a "cannot find server or DNS" error message. This seems like an unsolvable problem restricted by the Protocol. Just as I was about to leave, I accidentally saw an article. According to the solution I wrote, this problem was actually solved. It was really a treasure of the mountains and waters, and it was just like a village.
Now I will sort out the solution based on my ideas. I will summarize it and hope it will help others. This is certainly true for Kong Ming afterwards, but after all, there are many benefits.
As mentioned earlier Application_error Event Processing captured" Maximum request length exceeded. "The error is too late, and the page is no longer normal. Redirect To Custom error page Therefore, we should try to prevent the system from throwing" Maximum request length exceeded. "Exception. To prevent the system from throwing this exception Httphandler Processing Request Before, make Httphandler Obtained Httpcontext Content less Maxrequestlength . It seems that Global. asax In Application_beginrequest In the event processing process. The followingCodeThe solution is provided:
1 Private Void Application_beginrequest (Object source, eventargs E)
2 {
3 Httprequest request = Httpcontext. Current. request;
4 If (Request. contentlength > 4096000 ) // 4096000 is maxrequestlength
5 {
6 Httpapplication app = Source As Httpapplication;
7 Httpcontext Context = App. context;
8
9 Httpworkerrequest WR =
10 (Httpworkerrequest) (context. GetType (). getproperty
11 ( " Workerrequest " , Bindingflags. Instance |
12 Bindingflags. nonpublic). getvalue (context, Null ));
13 Byte [] Buffer;
14 If (WR. hasentitybody ())
15 {
16 Int Contentlen = Convert. toint32 (WR. getknownrequestheader (
17
18 Httpworkerrequest. headercontentlength ));
19 Buffer = Wr. getpreloadedentitybody ();
20 Int Received = Buffer. length;
21 Int Totalrecv = Received;
22 If ( ! Wr. isentireentitybodyispreloaded ())
23 {
24 Buffer = New Byte [ 65535 ];
25 While (Contentlen - Totalrecv > = Received)
26 {
27 Received =
28 Wr. readentitybody (buffer,
29 Buffer. Length );
30 Totalrecv + = Received;
31 }
32 Received =
33 Wr. readentitybody (buffer, contentlen - Totalrecv );
34 }
35 }
36 Context. response. Redirect ( " ../Error. aspx " ); // Redirect to custom error page.
37 }
From the code, we can see that the key idea is: if the uploaded file exceeds Maxrequestlength The uploaded file can be regarded as an invalid file, so you can safely and boldly slim down the file so that Request Of Httpcontext Content less Maxrequestlength In this way," Maximum request length exceeded. "The exception will not be generated, and the page can jump to normally Custom error page . The loop part in the code is to read the uploaded file content. Buffer Because the uploaded file is an invalid file, the file content is not important. Buffer Is only used to receive the content of the uploaded file, Buffer The data in does not need to be read again.
A seemingly unsolved problem is solved in this way. Although it is not your own strength, it still gains a lot, so naturally it is still very "beautiful "!