File download does not work with IE7 - asp.net

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.

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

Two things.
As mentioned before you will want to remove the spaces in the type
Is there any particular reason that you are not using Response.TransmitFile() rather than reading the file in yourself?

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

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();

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 .

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!!

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.

Related

How to set Browser when using Response.Write

My application is writing XML to a file
which is then downloaded (Response.Write) and the user can then open.
Question:
No matter what Browser I'm using (Chrome, Firefox,etc.) the downloaded file is
opened in IE.
I want the downloaded file to be opened in the browser which created it.
strXML = "<ROWSET></ROWSET>";
Response.Clear();
Response.Charset = "";
Response.ContentType = "text/xml";
Response.AddHeader("content-disposition", "attachment; filename=\"kupot.xml\"");
Response.Write(strXML);
Response.Flush();
Response.End();
Response.Close();
What should I do so that if the downloaded file was created in, for example, Chrome, then when it's downloaded and I'm asked if I want to open it, then if I answer YES then it'll be opened in Chrome
All help/ideas is appreciated.
Thanks
David
Drop the content disposition header. This tells the browser you want to download it as a raw file. This of course will be opened by whatever is associated with the XML file type on your system.
If you just want to return XML content directly to the browser use:
Response.ContentType = "text/xml";
Response.Write(strXML);
Response.Flush();
You also probably don't need to do Clear, Charset, End and Close.

download xml file using asp.net

I have created a xml file using c# and I can save it at any location in my machine (in this situation I have saved it on the root of the application with name "temp.xml"), but I want to let it for the user to download it from their browser buy giving a link like-->
"click HERE to download the file."
In Chrome and FireFox it show a new tab with only some values in my body part of the xml file but IE shows the whole xml. I want to download it in my download folder when anybody click on the above link.
thanks in advance for your support.
You need to specifically mention the file type and name. and use the TransmitFile method. This will show the Save As window.
I got the following code from this webpage
Response.ContentType = "image/jpeg";
Response.AppendHeader("Content-Disposition","attachment; filename=SailBig.jpg");
Response.TransmitFile( Server.MapPath("~/images/sailbig.jpg") );
HttpContext.Current.ApplicationInstance.CompleteRequest();
Response.AddHeader("content-disposition", "attachment;filename=temp.xml");
Response.ContentType = "text/xml";
Response.Write(File.ReadAllText(Server.MapPath("~/temp.xml"))); //you may want to write the XML directly to output stream instead of creating an XML file first.
Response.End();
Hope this helps.

Setting ContentType = "image/tiff" and sending an image is not working in IE

I need to send an image (as a downloadable file) from an ASP web page. It is working correctly in every browser except for IE (all versions).
Here is the server side code:
bool export = Request.QueryString["Export"] != null;
if (export)
{
byte[] allBytes = File.ReadAllBytes(#"C:\MyImage.tif");
Response.ContentType = "image/tiff";
Response.AddHeader("content-disposition", "attachment; filename=\"MyImage.tif\"");
Response.OutputStream.Write(allBytes, 0, allBytes.Length);
Response.OutputStream.Flush();
Response.End();
return;
}
And here is the JavaScript:
$('#ExportFrame').attr('src', 'Default.aspx?Export=true'); // ExportFrame is an iframe
In IE, I keep getting an error saying "Internet Explorer cannot download Default.aspx from localhost". I thought it might be an issue with loading it in an iframe element, but redirecting to the URL is not working either. The really odd thing is that going to the URL (/Default.aspx?Export=true) does not work the first time, but works every time after that. Again, this works in every browser I've tried except IE.
Any ideas?
Update:
The aspx page has the following code to keep the page from getting cached:
// Never cache this page
Response.CacheControl = "no-cache";
Response.AddHeader("Pragma", "no-cache");
Response.Expires = -1;
Removing the first 2 lines and leaving only Response.Expires = -1 resolved the issue.
AFAIK, you have to fix the file type association on the client for this to work. Known issue - Windows Updates often breaks this association resulting in this type of error message.
http://www.eggheadcafe.com/software/aspnet/36147698/office-updates-break-tiff-file-associations.aspx
UPDATE
1.) Create a blank text document on your desktop. (File.txt)
2.) Change the file extension from .txt to .tiff.
3.) Right click on the .tiff file and select open. On the next box choose “select program from a list of installed programs.
4.) Click on the browse button and browse to “C:\Program Files\Common Files\Microsoft Shared\MODI\12.0”.
5.) Select “MSPVIEW.EXE” and click “Open”.
6.) Select “Microsoft Office Document Imaging” and click OK.
7.) You will get a message saying it is unable to open the document. That is because it is not an actual graphic image. We are just setting the file association at this point.
8.) Repeat steps 1 – 7 and use .tif as apposed to .tiff because there are two different file extensions for this type of document.
The aspx page had the following code to keep the page from getting cached:
// Never cache this page
Response.CacheControl = "no-cache";
Response.AddHeader("Pragma", "no-cache");
Response.Expires = -1;
Removing the first 2 lines and leaving only Response.Expires = -1 resolved the issue. For some reason this was preventing the image from working properly in IE.

ASP.NET 4: HttpResponse open in NEW Browser?

I am using a PDF generater that utlizes HttpResponse. Is there a way (perhaps passing a header tag) to open the PDF in a NEW windows instead of the same one? I don't want the user to be directed away from the website...
Here's the code I'm using:
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response; response.Clear();
response.AddHeader("Content-Type", "application/pdf");
response.AddHeader("Content-Disposition",
"inline; filename=" + downloadName + "; size=" + downloadBytes.Length.ToString());
response.Flush();
response.BinaryWrite(downloadBytes);
response.Flush();
response.End();
You cannot do that on server side if you have not generated the link yourself. But if you have, then as Robert said, provide a target with the <a> link.
There is a server-side alternative and that is to set the content type to application/octect-stream so that the file is download and user will be able to open it with the application of choice outside browser. See here for more.
You also need to use content disposition header to provide the file name so that client can know what file type is it after it has been downloaded as binary.
Content-Disposition: attachment; filename=my.pdf;
You presumably have a link on your site that directs the user to the URL where this PDF generation is done. In that link, add target="_blank" to the <a> tag.
If what you're really asking is how to make the save dialog to show instead of having the PDF loaded into your browser, then you need to modify your header.
You are setting Content-Disposition to 'inline', change that to 'attachment'
http://support.microsoft.com/kb/260519

ASP.net text file download generation

I tried to look this up all over the place, but none of the examples given online helps me.
I basically want a button on an ASP.net page to generate some text in memory, and when the user clicks on it, display a download dialog for a user to download a text file with the generated text.
The code I have currently is:
Response.Clear();
Response.ClearHeaders();
Response.ContentType = "text/csv";
Response.AppendHeader("Content-Disposition", "attachment;filename=cartune.csv");
string text = "hello";
Response.Write(text);
Response.End();
But it doesn't work in Firefox 3.5 nor IE 8.0, I get no download response from the browsers at all... Many examples given online are similar to the code I have, so can any experts please point out to me what I am doing wrong?
I am running debugging mode in the Visual Studio 2008 deubugger, not IIS.
Thanks!
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("content-disposition", "attachment;filename=filename.csv");
Response.ContentType = "text/csv";
Response.Write("hello");
Response.End();
Works for me in Firefox 3.5. I just wrote this for my own project today.

Resources