I've tried using numerous questions/answers on SO - but can't seem to overcome the OutOfMemoryException I receive when trying to download a 200MB zip file via the web api.
I've drastically simplified my code in order to test:
[HttpPost]
public async Task<HttpResponseMessage> ExportReports(OrderExportFilter filterJson)
{
var filename = "C:\\pdftemp\\1128d0ff-a4b7-440d-9e3b-dd152445eb62.zip";
var fileStream = File.OpenRead(filename);
var content = new StreamContent(fileStream, 4096);
resp.Content = content;
resp.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");
resp.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
{
FileName = "OrdersExport.pdf"
};
return resp;
}
This is just one of many way's I've attempted to download the zip file, I've also referenced this link and duplicated the code to no avail.
I'm at a loss as to what I'm doing incorrectly to enable large zip files to be downloaded.
Update: Per request in comments i've tried using "application/octet-stream" instead - still no luck - same error.
Also - one more thing to note - when i download smaller zip files using this code it works fine, it just seems once the file is too large is starts to bomb.
Exception Update:
I was able to pull the exception details:
{
"Message": "An error has occurred.",
"ExceptionMessage": "Exception of type 'System.OutOfMemoryException' was thrown.",
"ExceptionType": "System.OutOfMemoryException",
"StackTrace": " at System.IO.MemoryStream.set_Capacity(Int32 value)\r\n at System.IO.MemoryStream.EnsureCapacity(Int32 value)\r\n at System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count)\r\n at System.IO.Compression.DeflateStream.WriteDeflaterOutput(Boolean isAsync)\r\n at System.IO.Compression.DeflateStream.PurgeBuffers(Boolean disposing)\r\n at System.IO.Compression.DeflateStream.Dispose(Boolean disposing)\r\n at System.IO.Stream.Close()\r\n at System.IO.Compression.GZipStream.Dispose(Boolean disposing)\r\n at System.IO.Stream.Close()\r\n at System.IO.Stream.Dispose()\r\n at System.Net.Http.Extensions.Compression.Core.Compressors.BaseCompressor.<Compress>d__4.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Net.Http.Extensions.Compression.Core.Models.CompressedContent.<SerializeToStreamAsync>d__4.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Owin.HttpMessageHandlerAdapter.<BufferResponseContentAsync>d__13.MoveNext()"
}