How to get Firefox to correctly identify file download? - asp.net

The following asp.net code works fine on Chrome & IE, but Firefox shows the file as MyDownload.zip.htm and hence doesn't know what to do with it.
//ASP.Net file download code
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=MyDownload.zip");
Response.TransmitFile(Server.MapPath("~/MyDownload.zip"));
Response.Flush();
Any ideas how to get Firefox to recognise the attachment correctly?

Add MIME type header:
Response.ContentType = "application/zip";

Related

Downloading is working fine in desktop browser but not in mobile browsers

I am new to ASP.NET, specially web based application. I have written a webpage for downloading the image from server. It works fine when I try to download from desktop browser, but it does not download if I try to download from iPad or Android tablets. In iPad It shows some encoded string while in case of Android, it downloads an HTML file of the same page. My code is as below :
string file = obj.Base_Url; // NAME OF THE FILE ON THE SERVER e.g; A.png
string imageUrl = Server.MapPath("~/Images" + '/' + file);
Response.AppendCookie(new HttpCookie("fileDownloadToken", download_token_value.Value));
Response.AddHeader("content-disposition", "attachment;filename=" + file);
Response.WriteFile(Server.MapPath("~/Images" + '/' + file));
Response.Flush();
Please help me. Thanks in advance..
Try adding Response.ContentType = "image/jpeg"; (or png/gif...) before the Response.WriteFile line.

IE 8 - HttpResponse - Can't display PDF

I am getting one weird issue with IE 8 only. In my app, we have write the binary response to the HttpResponse object and the data content is PDF. We are using third part tool "Tall PDF" to make PDF's. But this whole operation end up in freezing my IE and eventually had to end-task the IE instance..
Following is the code excerpts:-
Response.Clear();
Response.ContentType = "application/pdf";
nb.WritePDF(Response);
Response.Flush();
Response.End();
nb is the class and calling the method WritePDF to write the data to the Response obj. This thing is working fine in IE 7 and other browsers, but for some reason it is giving problem in IE8.
Pls suggest any work arounds or tweaks.
Please try the code snippet provided in the following blog post: Rendering PDF Files to Browser using .NET Code
i found the solution
response.AddHeader("X-UA-Compatible", "IE=EmulateIE7");

Response.BinaryWrite not working with Safari in MAC

I'm using the following code to send a PDF to the browser
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Length", fileBytes.Length.ToString());
Response.ContentType = "application/pdf";
Response.BinaryWrite(fileBytes);
Response.Flush();
System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest();
It works well (pdf is embedded properly within the browser) for all browsers except Safari (version 5.x) in MAC. Also, it is working properly for Firefox and Chrome in MAC.
I'm wondering is it a browser issue? or there is something wrong with Response.BinaryWrite?
[Edit]
Safari behavior, the pdf is not loaded at all. One progress bar is shown and keep loading, may be it's related to the PDF plugin which is installed? So, how to figure out the why this behavior in Safari?
[Edit]
This error is logged in Safari Developer Tools console "Failed to load resource: Plug-in handled load" which seems that it's related to the PDF plugin.
Try this, it's working with me fine
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.Buffer = true;
Response.AppendHeader("Accept-Header", attachmentObj.AttachmentFile.Length.ToString());
Response.AppendHeader("content-disposition", "attachment; filename=\"" + HttpUtility.UrlEncode(attachmentObj.Description, System.Text.Encoding.UTF8) + "\"");
Response.AppendHeader("Pragma", "public");
Response.BinaryWrite((byte[])attachmentObj.AttachmentFile.ToArray());
Response.Flush();
Response.End();

Displaying images and documents using asp.net across several browsers

I have a webpage called DisplayBinaryData.aspx - the purpose of this page being to display/download any word, excel, pdf or images. I call this webpage and pass the id of my BinaryData entity using a querystring. The BinaryData entity contains the file, filename and contenttype uploaded using the asp.net fileUploadControl. The code in the page load is below:
BinaryData obj = GetBinaryObjectById(int.Parse(Request.QueryString["id"]));
Response.Clear();
Response.BufferOutput = true;
Response.AddHeader("Content-Disposition", "attachment; filename=" + obj.FileName);
Response.ContentType = obj.FileContentType;
Response.BinaryWrite(obj.BinaryFile);
Response.Flush();
Response.Close();
Response.End();
This code executes perfect in IE,but fails when executed in FireFox. IE prompts the user, either to save or open the content. FireFox also prompts the user, but the dialog box fails to save or open any content. When executing this in google chrome - there is no dialog box, it starts downloading the content automatically.
My question: I need this code to be compatable with FireFox - any suggestions?
The behavior you mention with Chrome is just do to the default settings for Chrome. You can change those by going to the "Under the hood" tab of the Options window. Then select the "Ask Where to save each file before downloading" checkbox.
Does your obj.FileName have a space in the name?
See this post on "Content Disposition" in different browsers.
Content Disposition in different
browsers
Today I had to resolve an issue where
in different browsers the filed
dynamically generated download worked
very differently / at all
The setup, we had an xml file with a
custom extension, say .mj, which was
being served up by ASP. The HTTP
Header had a content disposition
header and a response type set.
Response.AddHeader "Content-Disposition", "attachment; filename=""our file.mj"""
Response.ContentType = "text/xml"
This worked fine in Internet Explorer, the
file was downloaded as "our file.mj".
However FireFox and Chrome acted very
differently, in FireFox the file was
downloaded as just "our", and Chrome
as "our file.xml". In FireFox it
appears that the issue is caused by
having a space in the file name, this
forum post by funkdaddu helped me on
this, so by removing the space FireFox
could now download the file as
"ourfile.mj". ...

Problem with CSV download to IE 6

I have a friend experiencing problems testing a web site of mine that provides a CSV export for a report. He says the CSV is output to the screen, and no file is created.
It works fine on my IIS7, Vista, and IE7 setup. I can't get more details at the moment, but I'd like to at least ask: is my code, below, for sending the CSV adequate to be browser/version independent?
Response.Clear();
Response.Buffer = true;
Response.ContentType = "text/csv";
string fileName = GetExportFileName();
Response.AddHeader("Content-Disposition", "filename=" + fileName);
dt.WriteCsv(Response.Output, false);
Response.End();
I'm building fileName to include a date with /separators, which may be causing a problem, but it doesn't affect my machine, and the / gets automagically replaced with _.
I have also used
Response.AddHeader("content-disposition", "attachment;filename=FileName.csv");
which adds the "attachment" text. You might try that to see if it makes any difference for your friend.
There is a known problem in IE6 that do not correctly support the content-disposition meta tag and there are some gotchas in how to compose the various meta tags.
This link from Scott Hanselman details everything about the issue.
Regards
Massimo
Very late to the party very here, so this is more for anyone who came across this issue at a later time. This is in relation to quote "No HTTP header will force IE6 to offer a download."
In PHP I have the headers set as the following for IE to show a 'Save' button, and not just download the file - or display the file in the brower window.
Response.AddHeader("X-Download-Options", "noopen");
Response.AddHeader("X-Content-Type-Options", "nosniff");
The X headers are used to indicate that it is a non-standard header type. Works in IE6.
The only sure fire way to get IE to prompt for a download is to have the CSV file compressed (ie file.csv.zip or file.zip).
See
http://blogs.msdn.com/ie/archive/2005/02/01/364581.aspx
Handling MIME Types in Internet Explorer
MIME Type Detection in Internet Explorer
No HTTP header will force IE6 to offer a download. This is especially true for text content, which is 'auto-detected' by IE.
You can try with Content-Disposition attachment; filename=file.csv but I'm not certain it'll always work. If it does, please confirm.
You didn't mention what browser your friend is using. In the end, it could be a bug or non-standard behavior.
The following technique seems to work fine for me: http://www.blackbeltcoder.com/Articles/asp/creating-downloadable-content-dynamically.

Resources