views:

3548

answers:

9

I have an asp.net page which sends content of a file to the client, so the browser shows the save as dialog to download the file. This page is displayed in a popup and when the user clicks the save button, it closes automatically and the download starts.

On windows server 2003, it works fine. On vista with other browsers, also works fine. But when I try with IE7 & Vista, the popup opens, and closes after about a second without displaying the file download dialog. How can I solve this?

The code I use for response generation is:

FileStream fileStream = new FileStream(filePath, FileMode.Open);
int fileSize = (int)fileStream.Length;

byte[] buffer = new byte[fileSize];
fileStream.Read(buffer, 0, (int)fileSize);
fileStream.Close();

Response.Clear();

Response.Buffer = true;
Response.BufferOutput = true;
Response.ContentType = "application / octet - stream";

Response.AddHeader("Content-Length", buffer.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
Response.AddHeader("Extension", Path.GetExtension(filename));
Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1254"); 
Response.BinaryWrite(buffer);
Response.Flush();
Response.End();

And I am opening the popup with this javascript:

window.open ('Download.aspx?filename=somefile.ext','downloadWindow','location=0,status=0,scrollbars=0,width=1,height=1');

EDIT: I corrected the spaces but unfortunately they are not the problem.

EDIT 2:: Seems that this problem is not related to Vista but IE only. I also discovered that it works fine when the project is run on the development server locally but when working as connected to publish server, it fails to download the file.

+8  A: 

Try removing the spaces in your ContentType. The standard is application/octet-stream.

swilliams
+1  A: 

I can't point to a specific problem in your code (except possibly for that content type, which looks badly-formed; not sure if that makes a difference). Here's the code I use for this, which works in both IE7 and Firefox:

Response.ContentType = "application/x-download";
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.CacheControl = "public";
Response.OutputStream.Write(byteArr, 0, byteArr.Length);
Response.End();
DannySmurf
+2  A: 

Two things.

  1. As mentioned before you will want to remove the spaces in the type
  2. Is there any particular reason that you are not using Response.TransmitFile() rather than reading the file in yourself?
Mitchel Sellers
+2  A: 

I'd also suggest you add quotes around the file name, otherwise, if it contains spaces, it will get truncated in Firefox.

Carl
A: 

I think your problem could be with IIS 7 . There is a problem with "addHeader" in the new Internet information Server with the integration pipeline mode.

Try to use Response.AppendHeader .

A: 

Hi! I also had the same problem...and I used this solution (I'm using it on a button.click):

Response.ContentType = "text/txt";
Response.AppendHeader("Content-Disposition", "attachment; filename="+DownloadFileName);
Response.Write(MyFileContent_Text_);
Response.End();

...it just worked!!

A: 

Hi Did you get rid of this issue ? If so could you share the solution with us.

A: 

I am experiencing the same issue and I'm stuck with the default IE7 security settings. Does anyone have a solution?

A: 

Hello, I came across this post because I was having a similar problem if not the same one. I am running IE8 on Windows 7.

When debugging on my local machine I could get the File Download prompt to display, but when clicking "Save" or "Open" the Download Progress window would display for about a half second and then close suddenly without downloading anything.

I have an add-on installed for Internet Explorer called IE7Pro. It comes with a Download Manager which I had enabled. When I disabled it, my problems went away and I could Open or Save my files.

Hope this proves helpful to someone else out there.

Airn5475