fuxuan1982929

解决上传文件大小限制问题

我们知道在默认的情况下ASP.NET的文件的上传大小为2M,一般情况下,我们可以采用更改web.config方式来定义最大文件的大小,如下: 

 

 

<httpRuntime executionTimeout="300" maxRequestLength="40960" useFullyQualifiedRedirectUrl="false" />

 

 

但是这样并不能无限制的扩大,因为ASP.NET会将全部的文件载入到内存后再进行处理。

      

解决方法是利用隐含的HttpWorkerRequest,用它的GetPreloadedEntityBody和ReadEntityBody方法从IIS为ASP.NET建立PIPE里面分块读取。

 

如下:

       

void test()
{
IServiceProvider provider 
= (IServiceProvider)HttpContext.Current;
HttpWorkerRequest wr 
= (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));

byte[] bs = wr.GetPreloadedEntityBody();
if(!wr.IsEntityBodyIsPreloaded())
{
int n = 1024;
byte[] bs2 = new byte[n];
while(wr.ReadEntityBody(bs2,n)>0)
{
   
//取得资料
}

}

}

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2022-03-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-09
猜你喜欢
  • 2022-12-23
  • 2021-06-20
  • 2022-02-14
  • 2022-12-23
  • 2021-10-16
  • 2022-02-11
  • 2022-12-23
相关资源
相似解决方案