自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

在ASP.NET MVC中實現(xiàn)大文件異步上傳

原創(chuàng)
開發(fā) 后端
在ASP.NET MVC中,我們使用StaticWorkerRequest建立虛假聲明的方式實現(xiàn)大文件上傳,現(xiàn)在你可以在ASP.NET MVC中通過直接訪問數(shù)據流上傳大文件,同時還可保證內存資源的消耗相對平穩(wěn)。

【51CTO獨家特稿】在ASP.NET中通過HTTP上傳大文件是一個由來已久的挑戰(zhàn),它是許多活躍的ASP.NET論壇最常討論的問題之一,除了處理大文件外,用戶還經常被要求要顯示出文件上傳的進度,當你需要直接控制從瀏覽器上傳數(shù)據流時,你會四處碰壁。51CTO.com之前就曾針對性的報道過《解除ASP.NET上傳文件的大小限制》和《ASP.NET大文件上傳開發(fā)總結》等文章。

絕大多數(shù)人認為在ASP.NET中上傳大文件有以下這些解決方案:

◆不要這樣做。你***是在頁面中嵌入一個Silverlight或Flash進程上傳文件。

◆不要這樣做。因為HTTP本身設計就不是為了上傳大文件,重新思考你要的功能。

◆不要這樣做。ASP.NET本身設計***也就能處理2GB大小的文件。

◆購買商業(yè)產品,如SlickUpload,它使用了一個HttpModule實現(xiàn)了文件流分塊。

◆使用開源產品,如NeatUpload,它使用了一個HttpModule實現(xiàn)了文件流分塊。

最近我接到一個任務,需構建一個上傳工具實現(xiàn)以下功能:

◆必須工作在HTTP協(xié)議

◆必須允許非常大的文件上傳(會大于2GB)

◆必須允許斷點續(xù)傳

◆必須允許并行上傳

因此前三個解決方案都不適應我的需求,其它解決方案對于我而言又太笨重了,因此我開始著手解決在ASP.NET MVC中的這個問題,如果有這方面的開發(fā)背景,你一定了解大部分問題最終都歸結于對ASP.NET輸入流和連鎖請求過程的控制,網上的資料一般都是這樣描述的,只要你的代碼訪問了HttpRequest的InputStream屬性,在你訪問流之前,ASP.NET就會緩存整個上傳的文件,這就意味著當我向云服務上傳文件時,我必須等待整個大文件抵達服務器,然后才能將其傳輸?shù)筋A定目的地,這意味著需要兩倍的時間。

首先,我們推薦你閱讀一下Scott Hanselman的有關ASP.NET MVC文件上傳文章,地址http://www.hanselman.com/blog/CommentView.aspx?guid=bc137b6b-d8d0-47d1-9795-f8814f7d1903,先對文件上傳有一個大致的了解,但Scott Hanselman的方法是不能上傳大文件的,根據Scott Hanselman的方法,你只需要修改一下web.config文件,確保ASP.NET允許***支持2GB大小的文件上傳,不要擔心,這樣設置并不會吃掉你的內存,因為凡是大于256KB的數(shù)據都被緩存到磁盤上去了。

  1. ﹤system.web﹥  
  2. ﹤httpruntime requestlengthdiskthreshold="256" maxrequestlength="2097151"﹥  
  3. ﹤/httpruntime﹥﹤/system.web﹥ 

這是一個簡單的適合大多數(shù)應用的解決辦法,但我的任務中不能借用這種方法,即使會將數(shù)據緩存到磁盤中,但這種類似于另存為的方法也會使用大量的內存。

通過緩存整個文件的方式,內存消耗突然上升 
圖 1 :通過緩存整個文件,然后另存為的方式會使內存消耗突然上升

那么在ASP.NET MVC中通過直接訪問流,不觸發(fā)任何緩存機制,上傳大文件該如何實現(xiàn)呢?解決辦法就是盡量遠離ASP.NET,我們先來看一看UploadController,它有三個行為方法,一個是索引我們上傳的文件,一個是前面討論的緩存邏輯,另一個是基于實時流的方法。

  1. public class UploadController : Controller  
  2. {  
  3.     [AcceptVerbs(HttpVerbs.Get)]  
  4.     [Authorize]  
  5.     public ActionResult Index()  
  6.     {  
  7.         return View();  
  8.     }  
  9.  
  10.     [AcceptVerbs(HttpVerbs.Post)]  
  11.     public ActionResult BufferToDisk()  
  12.     {  
  13.         var path = Server.MapPath("~/Uploads");  
  14.  
  15.         foreach (string file in Request.Files)  
  16.         {  
  17.             var fileBase = Request.Files[file];  
  18.  
  19.             try 
  20.             {  
  21.                 if (fileBase.ContentLength > 0)  
  22.                 {  
  23.                     fileBase.SaveAs(Path.Combine(path, fileBase.FileName));  
  24.                 }  
  25.             }  
  26.             catch (IOException)  
  27.             {  
  28.  
  29.             }  
  30.         }  
  31.  
  32.         return RedirectToAction("Index""Upload");  
  33.     }  
  34.  
  35.     //[AcceptVerbs(HttpVerbs.Post)]  
  36.     //[Authorize]  
  37.     public void LiveStream()  
  38.     {  
  39.         var path = Server.MapPath("~/Uploads");  
  40.  
  41.         var context = ControllerContext.HttpContext;  
  42.  
  43.         var provider = (IServiceProvider)context;  
  44.  
  45.         var workerRequest = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));  
  46.  
  47.         //[AcceptVerbs(HttpVerbs.Post)]  
  48.         var verb = workerRequest.GetHttpVerbName();  
  49.         if(!verb.Equals("POST"))  
  50.         {  
  51.             Response.StatusCode = (int)HttpStatusCode.NotFound;  
  52.             Response.SuppressContent = true;  
  53.             return;  
  54.         }  
  55.  
  56.         //[Authorize]  
  57.         if(!context.User.Identity.IsAuthenticated)  
  58.         {  
  59.             Response.StatusCode = (int)HttpStatusCode.Unauthorized;  
  60.             Response.SuppressContent = true;  
  61.             return;  
  62.         }  
  63.  
  64.         var encoding = context.Request.ContentEncoding;  
  65.  
  66.         var processor = new UploadProcessor(workerRequest);  
  67.  
  68.         processor.StreamToDisk(context, encoding, path);  
  69.  
  70.         //return RedirectToAction("Index", "Upload");  
  71.         Response.Redirect(Url.Action("Index""Upload"));  
  72.     }  
  73. }  

雖然這里明顯缺少一兩個類,但基本的方法還是講清楚了,看起來和緩存邏輯并沒有太大的不同之處,我們仍然將流緩存到了磁盤,但具體處理方式卻有些不同了,首先,沒有與方法關聯(lián)的屬性,謂詞和授權限制都被移除了,使用手動等值取代了,使用手工響應操作而不用ActionFilterAttribute聲明的原因是這些屬性涉及到了一些重要的ASP.NET管道代碼,實際上在我的代碼中,我還特意攔截了原生態(tài)的HttpWorkerRequest,因為它不能同時做兩件事情。

#p#

HttpWorkerRequest有VIP訪問傳入的請求,通常它是由ASP.NET本身支持工作的,但我們綁架了請求,然后欺騙剩下的請求,讓它們誤以為前面的請求已經全部得到處理,為了做到這一點,我們需要上面例子中未出現(xiàn)的UploadProcessor類,這個類的職責是物理讀取來自瀏覽器的每個數(shù)據塊,然后將其保存到磁盤上,因為上傳的內容被分解成多個部分,UploadProcessor類需要找出內容頭,然后拼接成帶狀數(shù)據輸出,這一可以在一個上傳中同時上傳多個文件。

  1. internal class UploadProcessor  
  2. {  
  3.     private byte[] _buffer;  
  4.     private byte[] _boundaryBytes;  
  5.     private byte[] _endHeaderBytes;  
  6.     private byte[] _endFileBytes;  
  7.     private byte[] _lineBreakBytes;  
  8.  
  9.     private const string _lineBreak = "\r\n";  
  10.  
  11.     private readonly Regex _filename =  
  12.         new Regex(@"Content-Disposition:\s*form-data\s*;\s*name\s*=\s*""file""\s*;\s*filename\s*=\s*""(.*)""",  
  13.                   RegexOptions.IgnoreCase | RegexOptions.Compiled);  
  14.  
  15.     private readonly HttpWorkerRequest _workerRequest;  
  16.  
  17.     public UploadProcessor(HttpWorkerRequest workerRequest)  
  18.     {  
  19.         _workerRequest = workerRequest;  
  20.     }  
  21.  
  22.     public void StreamToDisk(IServiceProvider provider, Encoding encoding, string rootPath)  
  23.     {  
  24.         var buffer = new byte[8192];  
  25.  
  26.         if (!_workerRequest.HasEntityBody())  
  27.         {  
  28.             return;  
  29.         }  
  30.  
  31.         var total = _workerRequest.GetTotalEntityBodyLength();  
  32.         var preloaded = _workerRequest.GetPreloadedEntityBodyLength();  
  33.         var loaded = preloaded;  
  34.  
  35.         SetByteMarkers(_workerRequest, encoding);  
  36.  
  37.         var body = _workerRequest.GetPreloadedEntityBody();  
  38.         if (body == null// IE normally does not preload  
  39.         {  
  40.             body = new byte[8192];  
  41.             preloaded = _workerRequest.ReadEntityBody(body, body.Length);  
  42.             loaded = preloaded;  
  43.         }  
  44.  
  45.         var text = encoding.GetString(body);  
  46.         var fileName = _filename.Matches(text)[0].Groups[1].Value;  
  47.         fileName = Path.GetFileName(fileName); // IE captures full user path; chop it  
  48.  
  49.         var path = Path.Combine(rootPath, fileName);  
  50.         var files = new List {fileName};  
  51.         var stream = new FileStream(path, FileMode.Create);  
  52.  
  53.         if (preloaded > 0)  
  54.         {  
  55.             stream = ProcessHeaders(body, stream, encoding, preloaded, files, rootPath);  
  56.         }  
  57.  
  58.         // Used to force further processing (i.e. redirects) to avoid buffering the files again  
  59.         var workerRequest = new StaticWorkerRequest(_workerRequest, body);  
  60.         var field = HttpContext.Current.Request.GetType().GetField("_wr", BindingFlags.NonPublic | BindingFlags.Instance);  
  61.         field.SetValue(HttpContext.Current.Request, workerRequest);  
  62.  
  63.         if (!_workerRequest.IsEntireEntityBodyIsPreloaded())  
  64.         {  
  65.             var received = preloaded;  
  66.             while (total - received >= loaded && _workerRequest.IsClientConnected())  
  67.             {  
  68.                 loaded = _workerRequest.ReadEntityBody(buffer, buffer.Length);  
  69.                 stream = ProcessHeaders(buffer, stream, encoding, loaded, files, rootPath);  
  70.  
  71.                 received += loaded;  
  72.             }  
  73.  
  74.             var remaining = total - received;  
  75.             buffer = new byte[remaining];  
  76.  
  77.             loaded = _workerRequest.ReadEntityBody(buffer, remaining);  
  78.             stream = ProcessHeaders(buffer, stream, encoding, loaded, files, rootPath);  
  79.         }  
  80.  
  81.         stream.Flush();  
  82.         stream.Close();  
  83.         stream.Dispose();  
  84.     }  
  85.  
  86.     private void SetByteMarkers(HttpWorkerRequest workerRequest, Encoding encoding)  
  87.     {  
  88.         var contentType = workerRequest.GetKnownRequestHeader(HttpWorkerRequest.HeaderContentType);  
  89.         var bufferIndex = contentType.IndexOf("boundary=") + "boundary=".Length;  
  90.         var boundary = String.Concat("--", contentType.Substring(bufferIndex));  
  91.  
  92.         _boundaryBytes = encoding.GetBytes(string.Concat(boundary, _lineBreak));  
  93.         _endHeaderBytes = encoding.GetBytes(string.Concat(_lineBreak, _lineBreak));  
  94.         _endFileBytes = encoding.GetBytes(string.Concat(_lineBreak, boundary, "--", _lineBreak));  
  95.         _lineBreakBytes = encoding.GetBytes(string.Concat(_lineBreak + boundary + _lineBreak));  
  96.     }  
  97.  
  98.     private FileStream ProcessHeaders(byte[] buffer, FileStream stream, Encoding encoding, int count, ICollection files, string rootPath)  
  99.     {  
  100.         buffer = AppendBuffer(buffer, count);  
  101.  
  102.         var startIndex = IndexOf(buffer, _boundaryBytes, 0);  
  103.         if (startIndex != -1)  
  104.         {  
  105.             var endFileIndex = IndexOf(buffer, _endFileBytes, 0);  
  106.             if (endFileIndex != -1)  
  107.             {  
  108.                 var precedingBreakIndex = IndexOf(buffer, _lineBreakBytes, 0);  
  109.                 if (precedingBreakIndex > -1)  
  110.                 {  
  111.                     startIndex = precedingBreakIndex;  
  112.                 }  
  113.  
  114.                 endFileIndex += _endFileBytes.Length;  
  115.  
  116.                 var modified = SkipInput(buffer, startIndex, endFileIndex, ref count);  
  117.                 stream.Write(modified, 0, count);  
  118.             }  
  119.             else 
  120.             {  
  121.                 var endHeaderIndex = IndexOf(buffer, _endHeaderBytes, 0);  
  122.                 if (endHeaderIndex != -1)  
  123.                 {  
  124.                     endHeaderIndex += _endHeaderBytes.Length;  
  125.  
  126.                     var text = encoding.GetString(buffer);  
  127.                     var match = _filename.Match(text);  
  128.  
  129.                     var fileName = match != null ? match.Groups[1].Value : null;  
  130.                     fileName = Path.GetFileName(fileName); // IE captures full user path; chop it  
  131.  
  132.                     if (!string.IsNullOrEmpty(fileName) && !files.Contains(fileName))  
  133.                     {  
  134.                         files.Add(fileName);  
  135.  
  136.                         var filePath = Path.Combine(rootPath, fileName);  
  137.  
  138.                         stream = ProcessNextFile(stream, buffer, count, startIndex, endHeaderIndex, filePath);  
  139.                     }  
  140.                     else 
  141.                     {  
  142.                         var modified = SkipInput(buffer, startIndex, endHeaderIndex, ref count);  
  143.                         stream.Write(modified, 0, count);  
  144.                     }  
  145.                 }  
  146.                 else 
  147.                 {  
  148.                     _buffer = buffer;  
  149.                 }  
  150.             }  
  151.         }  
  152.         else 
  153.         {  
  154.             stream.Write(buffer, 0, count);  
  155.         }  
  156.  
  157.         return stream;  
  158.     }  
  159.  
  160.     private static FileStream ProcessNextFile(FileStream stream, byte[] buffer, int count, int startIndex, int endIndex, string filePath)  
  161.     {  
  162.         var fullCount = count;  
  163.         var endOfFile = SkipInput(buffer, startIndex, count, ref count);  
  164.         stream.Write(endOfFile, 0, count);  
  165.  
  166.         stream.Flush();  
  167.         stream.Close();  
  168.         stream.Dispose();  
  169.  
  170.         stream = new FileStream(filePath, FileMode.Create);  
  171.  
  172.         var startOfFile = SkipInput(buffer, 0, endIndex, ref fullCount);  
  173.         stream.Write(startOfFile, 0, fullCount);  
  174.  
  175.         return stream;  
  176.     }  
  177.  
  178.     private static int IndexOf(byte[] array, IList value, int startIndex)  
  179.     {  
  180.         var index = 0;  
  181.         var start = Array.IndexOf(array, value[0], startIndex);  
  182.  
  183.         if (start == -1)  
  184.         {  
  185.             return -1;  
  186.         }  
  187.  
  188.         while ((start + index) < array.Length)  
  189.         {  
  190.             if (array[start + index] == value[index])  
  191.             {  
  192.                 index++;  
  193.                 if (index == value.Count)  
  194.                 {  
  195.                     return start;  
  196.                 }  
  197.             }  
  198.             else 
  199.             {  
  200.                 start = Array.IndexOf(array, value[0], start + index);  
  201.  
  202.                 if (start != -1)  
  203.                 {  
  204.                     index = 0;  
  205.                 }  
  206.                 else 
  207.                 {  
  208.                     return -1;  
  209.                 }  
  210.             }  
  211.         }  
  212.  
  213.         return -1;  
  214.     }  
  215.  
  216.     private static byte[] SkipInput(byte[] input, int startIndex, int endIndex, ref int count)  
  217.     {  
  218.         var range = endIndex - startIndex;  
  219.         var size = count - range;  
  220.  
  221.         var modified = new byte[size];  
  222.         var modifiedCount = 0;  
  223.  
  224.         for (var i = 0; i < input.Length; i++)  
  225.         {  
  226.             if (i >= startIndex && i < endIndex)  
  227.             {  
  228.                 continue;  
  229.             }  
  230.  
  231.             if (modifiedCount >= size)  
  232.             {  
  233.                 break;  
  234.             }  
  235.  
  236.             modified[modifiedCount] = input[i];  
  237.             modifiedCount++;  
  238.         }  
  239.  
  240.         input = modified;  
  241.         count = modified.Length;  
  242.         return input;  
  243.     }  
  244.  
  245.     private byte[] AppendBuffer(byte[] buffer, int count)  
  246.     {  
  247.         var input = new byte[_buffer == null ? buffer.Length : _buffer.Length + count];  
  248.         if (_buffer != null)  
  249.         {  
  250.             Buffer.BlockCopy(_buffer, 0, input, 0, _buffer.Length);  
  251.         }  
  252.         Buffer.BlockCopy(buffer, 0, input, _buffer == null ? 0 : _buffer.Length, count);  
  253.         _buffer = null;  
  254.  
  255.         return input;  
  256.     }  

在處理代碼的中間位置,你應該注意到了另一個類StaticWorkerRequest,這個類負責欺騙ASP.NET,在點擊提交按鈕時,它欺騙ASP.NET,讓他認為沒有文件上傳,這是必需的,因為當上傳完畢時,如果我們要重定向到所需的頁面時,ASP.NET將會檢查到在HTTP實體主體中仍然有數(shù)據,然后會嘗試緩存整個上傳,于是我們兜了一圈又回到了原點,為了避免這種情況,我們必須欺騙HttpWorkerRequest,將它注入到HttpContext中,獲得請求開始部分的StaticWorkerRequest,它是唯一有用的數(shù)據。

  1. internal class StaticWorkerRequest : HttpWorkerRequest  
  2. {  
  3.     readonly HttpWorkerRequest _request;  
  4.     private readonly byte[] _buffer;  
  5.  
  6.     public StaticWorkerRequest(HttpWorkerRequest request, byte[] buffer)  
  7.     {  
  8.         _request = request;  
  9.         _buffer = buffer;  
  10.     }  
  11.  
  12.     public override int ReadEntityBody(byte[] buffer, int size)  
  13.     {  
  14.         return 0;  
  15.     }  
  16.  
  17.     public override int ReadEntityBody(byte[] buffer, int offset, int size)  
  18.     {  
  19.         return 0;  
  20.     }  
  21.  
  22.     public override byte[] GetPreloadedEntityBody()  
  23.     {  
  24.         return _buffer;  
  25.     }  
  26.  
  27.     public override int GetPreloadedEntityBody(byte[] buffer, int offset)  
  28.     {  
  29.         Buffer.BlockCopy(_buffer, 0, buffer, offset, _buffer.Length);  
  30.         return _buffer.Length;  
  31.     }  
  32.  
  33.     public override int GetPreloadedEntityBodyLength()  
  34.     {  
  35.         return _buffer.Length;  
  36.     }  
  37.  
  38.     public override int GetTotalEntityBodyLength()  
  39.     {  
  40.         return _buffer.Length;  
  41.     }  
  42.  
  43.     public override string GetKnownRequestHeader(int index)  
  44.     {  
  45.         return index == HeaderContentLength  
  46.                    ? "0" 
  47.                    : _request.GetKnownRequestHeader(index);  
  48.     }  
  49.  
  50.     // All other methods elided, they're just passthrough  

使用StaticWorkerRequest建立虛假的聲明,現(xiàn)在你可以在ASP.NET MVC中通過直接訪問數(shù)據流上傳大文件,使用這個代碼作為開始,你可以很容易地保存過程數(shù)據,并使用Ajax調用另一個控制器行為展示其進度,將大文件緩存到一個臨時區(qū)域,可以實現(xiàn)斷點續(xù)傳,不用再等待ASP.NET進程將整個文件緩存到磁盤上,同樣,保存文件時也不用消耗另存為方法那么多的內存了。

使用StaticWorkerRequest,內存消耗更平穩(wěn) 
圖 2: 內存消耗更平穩(wěn)

 

【更多關于ASP.NET上傳文件的介紹】

  1. 專訪微軟MVP衣明志:走進ASP.NET MVC 2框架開發(fā)
  2. ASP.NET大文件上傳方法淺析
  3. ASP.NET上傳文件面面觀
  4. ASP.NET上傳文件控件實例詳解
  5. ASP.NET多附件上傳和附件編輯的實現(xiàn)
責任編輯:佚名 來源: 51CTO.com
相關推薦

2009-07-21 15:38:31

2009-07-20 16:09:39

2009-07-21 16:05:58

ASP.NET大文件上

2009-07-22 10:13:31

異步ActionASP.NET MVC

2009-03-06 10:28:30

MVCASP.NET異步Action

2009-07-20 15:44:32

ASP.NET MVC

2015-03-03 13:15:19

ASP.NET大文件下載實現(xiàn)思路

2009-07-24 13:20:44

MVC框架ASP.NET

2009-06-12 09:24:34

ASP.NET窗體ASP.NET MVC

2009-12-21 10:05:10

ASP.NET MVC

2009-07-31 12:43:59

ASP.NET MVC

2024-05-20 13:06:18

2009-02-16 10:05:11

ActionMVCASP.NET

2009-02-17 09:22:14

ActionMVCASP.NET

2009-07-22 16:02:39

ASP.NET MVCPagedList

2009-07-28 14:47:18

ASP.NET MVC

2017-03-06 11:13:57

ASP.NETCoreMVC

2009-07-22 13:16:04

MvcAjaxPaneASP.NET MVC

2009-07-29 10:02:49

ASP.NET上傳

2009-09-24 09:26:22

ASP.NET MVC
點贊
收藏

51CTO技術棧公眾號