[英]ASP.NET MVC Error while downloading large file at Response.Flush()
我使用ASP.NET MVC 5.我在控制器中有一個名為Download()的函數來給用戶請求文件。 我必須在目錄中壓縮文件並將其作為一個zip文件提供。 這些文件很大(例如,需要支持~100 GB下載)
這是我試圖讓這個功能發揮作用
public ActionResult Download()
{
var fileName = Path.GetRandomFileName() + ".zip";
using (var zip = new ZipFile(fileName))
{
zip.CompressionLevel = CompressionLevel.BestCompression;
zip.UseZip64WhenSaving = Zip64Option.Always;
zip.AddDirectory(Server.MapPath(directory));
var output = new MemoryStream();
//zip.Save(output);
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
Response.Buffer = true;
Response.BufferOutput = true;
zip.Save(Response.OutputStream);
Response.Flush();
output.Seek(0, SeekOrigin.Begin);
return File(output, "application/zip", "Compilation.zip");
}
}
文件和目錄始終是常量,在客戶端中它只是一個按鈕。
到目前為止,我已經使用總共<1 GB的文件對其進行了測試,它確實有效。 但是,當文件總共> 1 GB時會拋出異常。 我有這個例外
Not enough storage is available to process this command.
(Exception from HRESULT: 0x80070008)
Source Error:
Line 50: Response.Flush();
我想知道,我是否錯過了MVC web.config設置或IIS管理器設置中的某些配置? 或者它純粹是一個實現問題,我應該實現zip文件/目錄和另一種方式下載的方法下載zip文件。
編輯:要驗證,我的磁盤仍有足夠的空間,所以我不知道為什么它抱怨沒有足夠的存儲空間。
編輯2:該功能通過使Response.BufferOutput = false;
。 然而,當這個函數被調用時,這件事就會產生緩存。 在頻繁調用時可能會花費一些性能。 我想知道存儲錯誤是否是由於緩沖區大小。
PS:這是堆棧跟蹤,如果它有幫助:
[COMException (0x80070008): Not enough storage is available to process this command. (Exception from HRESULT: 0x80070008)]
[HttpException (0x80004005): An error occurred while communicating with the remote host. The error code is 0x80070008.]
System.Web.Hosting.IIS7WorkerRequest.RaiseCommunicationError(Int32 result, Boolean throwOnDisconnect) +3415687
System.Web.Hosting.IIS7WorkerRequest.FlushCore(Boolean keepConnected, Int32 numBodyFragments, IntPtr[] bodyFragments, Int32[] bodyFragmentLengths, Int32[] bodyFragmentTypes) +9790118
System.Web.Hosting.IIS7WorkerRequest.FlushCachedResponse(Boolean isFinal) +413
System.Web.HttpResponse.UpdateNativeResponse(Boolean sendHeaders) +467
System.Web.HttpResponse.Flush(Boolean finalFlush, Boolean async) +152
System.Web.HttpResponse.Flush() +23
System.Web.HttpResponseWrapper.Flush() +14
Poort80SimpleApp.Controllers.HomeController.Download() in D:\SimpleApp\SimpleApp\Controllers\HomeController.cs:50
lambda_method(Closure , ControllerBase , Object[] ) +101
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +59
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +435
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +60
System.Web.Mvc.Async.ActionInvocation.InvokeSynchronousActionMethod() +76
System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +36
System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +73
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +136
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +49
System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +117
System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +323
System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +44
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +47
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +136
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +50
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +72
System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +185
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +42
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44
System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +39
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +62
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +39
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9742689
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
Response.BufferOutput = false;
return File(fileStream, contentType);
嘗試使用此組合,這將幫助您download
。 這可能需要一段時間,但文件將成功downloaded
。
我使用這種方法,我在2GB文件上測試它:
public FileResult BookCover(string id,string mimeType)
{
return new FilePathResult("~/yourpath/cover.png", mimeType);
//use this if you want to return byte[]
// return File(data, mimeType);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.