views:

1083

answers:

3

I'm facing a very strange problem in my ASP.NET Application.

When the user clicks the button that downloads a file, Internet Explorer / Chrome / Firefox shows the save dialog but the name of the file is the name of the ASPX Page (For example, if the page is named Download.aspx the download dialog shows the "file" Download.zip). Sometimes, when playing with MIME type the download dialog shows "Download.aspx". Seems that you're trying to download the page, but actually is the correct file.

This happens with ZIP extension and here is my code (pretty standard I think):


        this.Response.Clear();
        this.Response.ClearHeaders();
        this.Response.ClearContent();
        this.Response.AddHeader("Content–Disposition", "attachment; filename=" + file.Name);
        this.Response.AddHeader("Content-Length", file.Length.ToString());
        this.Response.ContentType = GETCONTENTYPE(System.IO.Path.GetExtension(file.Name));
        this.Response.TransmitFile(file.FullName);
        this.Response.End();

The GetContentType function just returns the MIME for the file. I tried with application/x-zip-compressed, multipart/x-zip and of course application/zip. With application/zip Internet Explorer 8 shows XML error.

Any help with be very appreciated.

Greetings,

+1  A: 

I think something like Response.Redirect(ResolveUrl(file.FullName)) instead of Response.TransmitFile(file.FullName) is what you intended. It sounds like you actually want their browser to point at the file, not just transmit the file as a response to the current the request.

Edit: Also see this SO question http://stackoverflow.com/questions/1384649/how-to-retrive-and-download-server-files-file-exists-and-url

Update: Based on your feedback, i think this is what you're looking for.

Chris Shouts
A: 

I'm looking at what I've done to handle a similar mechanism, and here's the steps I'm doing (bold item seemingly the only real difference):

Response.Clear();
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; // Excel 2007 format
// ... doing work...
Response.AddHeader("Content-Length", outputFileInfo.Length.ToString());
Response.TransmitFile(outputFileInfo.ToString());
HttpContext.Current.Response.End(); // <--This seems to be the only major difference

Although this.Response and HttpContext.Current.Response should be the same, it may not be for some reason.

Agent_9191
A: 

Thanks Agent_9191 and paper1337 for your answers.

I have to say that I don't want to use ResolveUrl because I want to keep secret the URL of the file. I want to transmit that file via URL.

Unfortunately, HttpContext.Current.Response.End() makes no difference with my code. That happens in my computer and in the web server.

Thanks a lot.

Jacob84