views:

890

answers:

5

I am dynamically generating a Zip file in an ASP.NET page and then sending the stream to Response. Pretty simple. In Firefox, I can download the file named "Images.zip" no problem. It works exactly as one might expect this to, so the code can't be too far wrong. 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'd rather not have to make an HTTPHandler for a certain file and have to register it with IIS. I am familiar with this though. Is there something simple I'm missing or is Internet Explorer at fault for ignoring my content-disposition header?

Edit: Moved code into ASHX page.

Response.Clear();
Response.End();

all great ideas, but no avail.

RECAP: I removed these lines and things worked:

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();

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();
}
+3  A: 

You should create an ASHX Handler for that. Have you tried using a content type of 'application/zip' instead?

Dan Diplo
Yes, I have. Same issue.
Chet
Good point about application/zip.
RichardOD
Also a good point about using an IHttpHandler.
RichardOD
+2  A: 

Instead of Response.ClearHeaders(), do a full Response.Clear(), and afterward, do a Response.End()

routeNpingme
Yes- not doing Response.Clear might be the cause.
RichardOD
Good ideas, but I don't think that really gets at the core issue.
Chet
+1  A: 

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.

RichardOD
I'm selecting this as the answer as it did solve the problem. After more testing the problem line appears to be the Response.Cache.SetCacheability(HttpCacheability.NoCache);
Chet
@Chet- that's good. Sometimes going back to a more basic version helps identify the issue.
RichardOD
Glad you got it sorted.
Dan Diplo
A: 

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();
BigBlondeViking
Thanks, yes, I considered that. The ZipFile.Save is a convenience method that the API advertises something to use for just this purpose. It works perfectly in Firefox, of course.
Chet
if IE worked like FF we would all be paid less... :)
BigBlondeViking
A: 

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.

Martin Smith