Reputation: 22105
I am dynamically generating a Zip file in a ASP.NET page and then sending the stream to Response.
In Firefox, I can download the file named Images.zip
. It works correctly. In Internet Explorer 7 it tries to download a file called ZipExport.aspx
or if it's in a Generic Handler, ZipExport.ashx
and it says it cannot be found on the server and fails.
Here's my code:
Response.BufferOutput = true;
Response.ClearHeaders();
Response.ContentType = "application/octet-stream";
Response.AddHeader("content-disposition", "attachment; filename=Images.zip");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoServerCaching();
Response.Cache.SetNoStore();
Response.Cache.SetMaxAge(System.TimeSpan.Zero);
ZipFile zip = new ZipFile();
zip.AddFile(Server.MapPath("sample1.png"));
zip.Save(Response.OutputStream);
I don't want to make an HTTPHandler for a certain file and register it with IIS.
Is there something simple I'm missing or is Internet Explorer at fault for ignoring my content-disposition header?
Edit: I removed these lines and things worked:
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
Edit: Here is the working code if anyone is interested:
public void ProcessRequest(HttpContext context)
{
context.Response.Clear();
context.Response.BufferOutput = false;
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("content-disposition",
"attachment; filename=ChartImages.zip");
context.Response.Cache.SetNoServerCaching();
context.Response.Cache.SetMaxAge(System.TimeSpan.Zero);
using(ZipFile zip = new ZipFile())
{
zip.AddFile(context.Server.MapPath("sample1.png"));
zip.Save(context.Response.OutputStream);
}
context.ApplicationInstance.CompleteRequest();
}
Upvotes: 1
Views: 4787
Reputation: 822
i just encountered the same issue and managed to fix
Response.Clear(); Response.BufferOutput = false;
Response.ContentType = "application/zip";
//Response.AddHeader("content-disposition", "inline; filename=\"" + ArchiveName + "\"");
Response.AddHeader("content-disposition", "attachment; filename=\"" + ArchiveName + "\"");
zipFile.Save(Response.OutputStream);
// Response.Close();
HttpContext.Current.ApplicationInstance.CompleteRequest();
Response.Clear();
Response.End();
Upvotes: 0
Reputation: 453898
I just encountered the same issue (and fix) thanks.
One point that might help future searchers is that the issue only arose for me on HTTPS sites. The code ran fine on my HTTP local server.
I guess with HTTPS it won't be cached anyway so can be enclosed in an "if(Request.IsSecureConnection)" condition.
Upvotes: 1
Reputation: 29157
Replace Response.End
with HttpContext.Current.ApplicationInstance.CompleteRequest
Try this cut down version:
Response.Clear();
Response.BufferOutput = false;
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment; filename=Images.zip");
using(ZipFile zip = new ZipFile())
{
zip.AddFile(Server.MapPath("sample1.png"));
zip.Save(Response.OutputStream);
}
HttpContext.Current.ApplicationInstance.CompleteRequest();
Failing that use Microsoft Fiddler to see what else might be going wrong.
Upvotes: 3
Reputation: 3963
I have never used ZipFile class, that being said when i send files i use Response.BinaryWrite()
//Adds document content type
context.Response.ContentType = currentDocument.MimeType;
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.AddHeader("content-disposition", "attachment;filename=\"" + currentDocument.Name + "\"");
//currentDocument.Document is the byte[] of the file
context.Response.BinaryWrite(currentDocument.Document);
context.Response.End();
Upvotes: 0
Reputation: 14196
Instead of Response.ClearHeaders(), do a full Response.Clear(), and afterward, do a Response.End()
Upvotes: 2
Reputation: 25359
You should create an ASHX Handler for that. Have you tried using a content type of 'application/zip' instead?
Upvotes: 3