Download PDF from aspx page

I have a page, when the user clicks a button, a PDF is dynamically generated and offered for download.

This is the code that allows the user to download pdf:

// Omitted code that generates the pdf bytes response.ContentType = "application/octetstream"; response.AppendHeader("Content-Disposition", "attachment; filename=" + filename); response.BinaryWrite(pdfBytes); response.End(); 

On my machine and many others using a mixture of Chrome, IE 7/8 / 9b and Firefox, this works as expected; user clicks a button, pdf loads.

In some cases of IE7, our users report that they receive an error message:

"Internet Explorer cannot download Publish.aspx from thesite.com

Internet Explorer was unable to open this website. The requested site is either unavailable or not found. Please try again later. "

Publish.aspx is the page the button is located on, so this page is accessible. IE should download the PDF file.

Is there something that doesn't match the code above that might be causing this on certain machines? Or does it depend on the specific security / OS / browser settings?

EDIT:

These are the response headers from the violinist:

 HTTP/1.1 200 OK Cache-Control: no-cache, no-store, must-revalidate Pragma: no-cache Content-Type: application/octetstream Expires: -1 Server: Microsoft-IIS/7.5 Content-Disposition: attachment; filename=myPdf.pdf X-AspNet-Version: 2.0.50727 X-Powered-By: ASP.NET Date: Fri, 12 Nov 2010 09:48:06 GMT Content-Length: 45772 
+4
source share
7 answers

More recently, I came across the same error. In my case, I used https and did not cache. It seems that the security feature in IE is not loading the file. From EricLaw IEInternals:

if the user tries to download the * file over an HTTPS connection, any response headers that prevent caching will lead to a failure of the file downloading process.

http://blogs.msdn.com/b/ieinternals/archive/2009/10/02/internet-explorer-cannot-download-over-https-when-no-cache.aspx

+2
source

Perhaps because the correct type is mime application/octet-stream , not application/octetstream .

+3
source

try using Response.OutputStream

 filepath= Server.MapPath(filepath); FileStream strm = new FileStream(filepath, FileMode.Open, FileAccess.Read); byte[] fileByte = new byte[strm.Length]; int x = strm.Read(fileByte, 0, fileByte.Length); Response.Clear(); Response.AddHeader("Accept-Header", fileByte.Length.ToString()); Response.AddHeader("Content-Disposition","inline; filename=" + filename); Response.ContentType = "application/pdf"; Response.OutputStream.Write(fileByte, 0, fileByte.Length); Response.Flush(); strm.Close(); 

and your content type should be = "application / pdf"

+2
source

Nicholas is right that "octetstream" (without a dash) is not known for the MIME Type .

I suggest using application/pdf .

+1
source

Does it matter if you use response.TransmitFile / response.WriteFile ?

TransmitFile (MSDN)
WriteFile (MSDN)

0
source

OK, I adjusted the content type for the application / octet stream and changed the caching. This seems to be an IE + SSL issue, so I will find out if it works when it is deployed later that evening. Thanks for the help.

0
source

found in google a solution to a similar problem:

 Response.AppendHeader('Expires', 'Sun, 17 Dec 1989 07:30:00 GMT'); 
-2
source

All Articles