How do I limit the size of the upload file in asp.net

  • 2020-05-16 06:38:28
  • OfStack

In web.config, where you control the size of the uploaded file:
 
<system.web><httpRuntime executionTimeout="9999" maxRequestLength="2097151"/></system.web> 

maxRequestLength is the maximum size (in kilobytes) of the parameter request that controls the upload size. The default size is 4096 KB (4 MB). ExecutionTimeout indicates the maximum number of seconds allowed before the request is automatically closed by ASP.NET. The default value is 90 seconds. It's in seconds. You can look at Msdn for details.

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

Resolve the asp.net upload file size limit
For asp.net, only 2m files are allowed to be uploaded by default. With the following configuration, you can customize the maximum file size.

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

If not, you can use the scheme provided by si GUI:

We've all had problems uploading large files one way or another. Setting a large maxrequestlength value does not completely solve the problem, because asp.net will block until the entire file is loaded into memory and then processed. In fact, if the file is very large, we will often see internet explorer display "the page cannot be displayed be find server or dns error". Why is that? Because this is an client side error,server side terminal 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 in chunks from the pipe built for iis for asp.net
 
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 articles: