How to restrict the size of files uploaded in asp.net

Source: Internet
Author: User

Where the size of the uploaded file is controlled in web. config:
Copy codeThe Code is as follows:
<System. web>
MaxRequestLength is the maximum size of the parameter request to control the upload size (in kilobytes ). The default size is 4096 KB (4 MB ). ExecutionTimeout indicates the maximum number of seconds that can be executed before the request is automatically disabled by ASP. NET. The default value is 90 seconds. The Unit is seconds. For details, refer to Msdn.

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

Solve the size limit of asp.net uploaded files
For asp.net, only 2 MB files can be uploaded by default. The following configuration is added. You can customize the maximum file size.

<Httpruntime executimaxrequestlength = "40960" usefullyqualifiedredirecturl = "false"/>

If not, you can use the solution provided by Si Gui:

We 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, the server side applicati

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.
Copy codeThe Code is 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)
{
.....
}
}

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.