ASP.NET MVC Error while downloading large file at Response.Flush()

4

I use ASP.NET MVC 5. I have a function called Download() in the controller to give the user request files. I have to zip files in the directory and deliver it as one zip file. These files are big (e.g, need to support ~100 GB download)

Here is my attempt to make that function work

    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");
        }
   }

The files and the directory is always constant, where in the client it is just a button.

So far I've tested it with files < 1 GB in total, it works. However, an exception gets thrown when the files are > 1 GB in total. I've got this exception

Not enough storage is available to process this command. 
(Exception from HRESULT: 0x80070008)
Source Error: 
Line 50:                    Response.Flush();

I wonder, did I miss some configuration in the MVC web.config settings or somewhere in the IIS Manager settings? Or is it purely an implementation problem that I should implement the zip files/directory and download zip file on the fly method the other way.

EDIT: To verify, My disk still have enough space so I don't know intuitively why it complains about not having enough storage.

EDIT 2: The function works by making the Response.BufferOutput = false; . However there is a consequence in this piece where there is cache anymore when this function is being called. Might've cost some performance when it is being called frequently. I wonder if the storage error is because of the buffer size.

PS: Here is the stack trace, if it helps:

[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
c#
asp.net
asp.net-mvc
large-files
iis-8
asked on Stack Overflow Jan 27, 2016 by haris hamdani • edited Jan 27, 2016 by haris hamdani

2 Answers

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

try using this combination this will help you for download. this may take while but file will be downloaded successfully.

answered on Stack Overflow Jan 9, 2019 by amit sarvalkar • edited Jan 9, 2019 by ArunPratap
0

I used this method and I tested it on 2GB file :

    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);           
    }
answered on Stack Overflow Jan 9, 2019 by Oday M. Saed • edited Jan 9, 2019 by Gilles-Antoine Nys

User contributions licensed under CC BY-SA 3.0