DavidGouge
DavidGouge

Reputation: 4623

PDF download from aspx page

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

This is the code that's letting the user download the 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 is working as expected; the user clicks the button, the PDF gets downloaded.

On some instances of IE7, our users are reporting that they are getting an error message:

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

Internet Explorer was not able to open this Internet site. The requested site is either not available or cannot be found. Please try again later".

Publish.aspx is the page that the button is residing on, so that page is available. IE should be downloading the pdf.

Is there anything that is wrong with the above code that could be causing this on certain machines? Or is it down to particular security / OS / browser settings?

EDIT:

These are the response headers from fiddler:

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

Upvotes: 4

Views: 11563

Answers (7)

Joop
Joop

Reputation: 2839

Very recently I bumped into the same error. In my case I was using https and no caching. It seems to be a security feature in IE to not download the file. From EricLaw's IEInternals:

if a user tries to download* a file over a HTTPS connection, any response headers that prevent caching will cause the file download process to fail.

Link

Upvotes: 2

DavidGouge
DavidGouge

Reputation: 4623

OK, I've corrected the content type to application/octet-stream and changed the caching. It seems to be an IE + SSL issue, so I'll find out if it works when it's deployed later this evening. Thanks for the help.

Upvotes: 0

PhilPursglove
PhilPursglove

Reputation: 12589

Does it make a difference if you use response.TransmitFile / response.WriteFile?

TransmitFile (MSDN)
WriteFile(MSDN)

Upvotes: 0

devio
devio

Reputation: 37215

Nicolas is correct that "octetstream" (without the dash) is not a known MIME Type.

I suggest using application/pdf.

Upvotes: 1

Aneef
Aneef

Reputation: 3729

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 must be ="application/pdf"

Upvotes: 2

x2.
x2.

Reputation: 9668

Found in google solution of similar problem:

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

Upvotes: -2

Nicolas Repiquet
Nicolas Repiquet

Reputation: 9265

That's maybe because the correct mime type is application/octet-stream, not application/octetstream.

Upvotes: 3

Related Questions