Data Browser - Viewing Site  Sector 23 Code Bank Logged in as:  Guest  


.NET Download Large Zip File
Downloading a large .zip file over .net (from stream or file path) uses up a lot of memory if you do it using Response.WriteFile/Response.WriteBytes.
Manually buffering it using Response.OutputStream.Write/Flush in chunks works, but was extremely slow on production server.
Use Response.TransmitFile (available in .NET 4.0, possibly 2.0?)
Note that this is asynchronous so don't delete the file after calling TransmitFile until you are sure the transmission has succeeded.

Note that you can pass in "application/zip" as the content type for a .zip file.
I tested this on firefox, IE 9, and chrome.

If you call Response.End(), there is an extraneous ThreadAbortException thrown (why? it's complicated).

But if you don't, the download won't complete in release mode.

/// <summary>
/// Download large file asynchronously using TransmitFile method of .NET.
/// </summary>
/// <param name="filepath">Identify the file to download including its path.</param>
/// <param name="contentType">Optional type if known.</param>
/// <param name="saveAsFileName">Optional type if known.</param>
public static void ExportLargeHttpFileAsync(string filepath, string contentType, string saveAsFileName)
if (!File.Exists(filepath))
return; // no file

// Identify the file name.
if (string.IsNullOrWhiteSpace(saveAsFileName))
saveAsFileName = System.IO.Path.GetFileName(filepath);

if (string.IsNullOrEmpty(contentType))
contentType = "application/octet-stream";
HttpContext.Current.Response.ContentType = contentType;
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + Path.GetFileName(saveAsFileName) + "\"");


Created By: amos 3/28/2013 11:49:25 AM
Updated: 4/1/2015 3:47:43 PM

 > Guest 2/5/2014 6:40:39 AM
Thank u so much:)