How to limit the size of uploaded files in asp.net code _ Practical Tips

Source: Internet
Author: User
Where to control the size of uploaded files in web.config:
Copy Code code as follows:

<system.web>

maxRequestLength is the maximum size (in kilobytes) of the request to control the upload size. The default size is 4096 KB (4 MB). Executiontimeout indicates the maximum number of seconds that are allowed to be performed before the request is asp.net automatically closed. The default value is 90 seconds. The unit is seconds. You can see MSDN in detail.

Http://msdn.microsoft.com/zh-cn/library/system.web.configuration.httpruntimesection.maxrequestlength%28v=VS.80%29.aspx

Resolve asp.net upload file size limit
For ASP.net, the default is to upload only 2m files, add the following configuration, you can generally customize the maximum file size.


If not, you can use the scenario provided by Shi:

We have had this or that problem when uploading large files. Setting a large maxRequestLength value does not completely solve the problem, because ASP.net blocks until the entire file is loaded into memory and then processed. In fact, if the file is very large, we often see Internet Explorer display "The page cannot be Displayed-cannot find server or DNS error", as if it could not catch this error 。 Why? Because this is a client side error, the server side end of the Applicati

Handling server error when upload file too large

The solution is to use the implied httpworkerrequest, using its getpreloadedentitybody and Readentitybody methods to read data from the pipe built by IIS for asp.net
Copy Code code as follows:

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)
{
.....
}
}

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.