在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
在大文件上传中,由于IIS对内存的保护机制,使得HttpWorkerRequest还没有转换成Request就由于内存占用太大而被终止了,所以我们在要可以获得HttpWorkRequest的时候将这里面的数据写到磁盘上,这样就不会使内存超出范围了。 1 application.BeginRequest += (new EventHandler(this.Application_BeginRequest)); 2 3 protected void Application_BeginRequest(object sender, EventArgs e) 4 { 5 HttpApplication ha = sender as HttpApplication; 6 HttpContext context = ha.Context; 7 8 if (ha.Context.Request.ContentLength > 1000)//1000 is the max size 9 { 10 IServiceProvider provider = (IServiceProvider)context; 11 HttpWorkerRequest wr = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest)); 12 //FileStream fs = null; 13 // Check if body contains data 14 if (wr.HasEntityBody()) 15 { 16 // get the total body length 17 int requestLength = wr.GetTotalEntityBodyLength(); 18 // Get the initial bytes loaded 19 int initialBytes = 0; //wr.GetPreloadedEntityBody().Length; 20 21 if (!wr.IsEntireEntityBodyIsPreloaded()) 22 { 23 byte[] buffer = new byte[100]; 24 // Set the received bytes to initial bytes before start reading 25 int receivedBytes = initialBytes; 26 while (requestLength - receivedBytes >= initialBytes) 27 { 28 // Read another set of bytes 29 initialBytes = wr.ReadEntityBody(buffer, buffer.Length); 30 // Write the chunks to the physical file 31 32 // Update the received bytes 33 receivedBytes += initialBytes; 34 } 35 initialBytes = wr.ReadEntityBody(buffer, requestLength - receivedBytes); 36 37 } 38 } 39 //fs.Flush(); 40 //fs.Close(); 41 } 42 }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论