在Response.Flush()下载大文件时出现ASP.NET MVC错误

时间:2016-01-27 11:19:06

标签: c# asp.net asp.net-mvc large-files iis-8

我使用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

2 个答案:

答案 0 :(得分:0)

Response.BufferOutput = false;
return File(fileStream, contentType);

尝试使用此组合可以帮助您download。 这可能需要一段时间,但文件将成功downloaded

答案 1 :(得分:0)

我使用了这种方法,并在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);           
    }