Response.BinaryWrite not working with Safari in MAC - asp.net

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

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.

How to get Firefox to correctly identify file download?

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";

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

IE not showing PDFs with caching disabled

I've been asked to implement a security requirement that we instruct browsers not to cache sensitive data. This is all fine for the ASPX content using the standard instructions:
Response.Expires = -1;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
However when I set these headers for PDF downloads, IE8 won't show the PDF (haven't tried other IE versions yet, kinda moot, I need it working on all of them, even IEfreaking6). Seems to work in firefox 4 beta, but I haven't double checked that it's definitely not caching it. Here is the abridged version of the code I'm using to serve the PDFs:
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.Buffer = true;
//This stops the PDFs from being viewed :(
//Response.Expires = -1;
//Response.Cache.SetCacheability(HttpCacheability.NoCache);
//Response.Cache.SetNoStore();
Response.ContentType = mime;
Response.AddHeader("Content-Disposition", disposition);
Response.BinaryWrite(file);
Response.End();
Where in the case of PDFs the mime type is set to:
private const string mimeTypePDF = "application/pdf";
The disposition is set to:
var disposition = String.Format("{0};filename=\"{1}\"", SendInline ? "inline" : "attachment", Path.GetFileName(filename));
I'm going to play-around a bit more, maybe forcing them to download as mimetype "application/octet-stream" might work, but that would stop the nice open PDF's in a new browser window from working.
Has anyone had any success with preventing IE from caching PDFs from the server side and successfully displaying them?
Just to give a clear example about what happens. In one scenario user's can select a bunch of reports from a list, these are compiled into a PDF and the PDF is shown in a new browser window. With the caching enabled the browser window opens, but remains resolutely blank.
I've had the same problem with IE a few years ago and let is cache since I didn't have a requirement to disallow it.
However, since users are free to save a PDF document once the browser shows it, how do you plan on prevent them from do that?
Not that this will solve your problem but when sending physical files you should Response.TransmitFile instead of BinaryWrite. It's much faster and more efficient in terms of memory utilization since you don't need to load the entire file in memory before sending it.
Looks like this issue has been resolved in IE9.
I can now successfully execute the following:
Response.Expires = -1
Response.Cache.SetNoStore()
Response.AppendHeader("Pragma", "no-cache")
Response.ContentType = "application/pdf"
Response.BinaryWrite(myByteBuffer)
Response.Flush()
Response.Close()
Enjoy!
I'm going to say it's not currently possible, nothing I tried seemed to get it to work. Try and get your customers to use firefox instead! :)
I also believe it's a bug in Internet Explorer. I was setting the cache-control header to no-cache and having the same problem. Also note that in Internet Options > Advanced > Security there's a 'Do not save encrypted pages to disk' option that could affect it.
Removal of the cache-control header from the response sorted out my issue. I also then tried checking the above mentioned option, and it seemed to work even better for me. Iinstead of storing the PDF in %LocalAppData%\Microsoft\Windows\Temporary Internet Files, it actually caused IE8 to issue a dialog allowing me to choose where to save it (which is actually what I wanted in my case).
This issue of showing pdf (and other document types) in-browser with the use of the no-cache header has been filed as a bug to Microsoft: http://support.microsoft.com/kb/316431. When you try to open a document in this case, IE tries to read it from cache, but it isn't there.
Unfortunately, the folks at M$ said this "works as designed" and users should not use the no-cache header... go figure.

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". ...

Resources