tags:

views:

405

answers:

2

Hi.

Say i have

  • a webpage with an iframe:
  • an url pointing to a pdf document: http://www.example.com
  • some javascript that will do iframe.src=pdfurl
  • a button that will trigger such javascript

    • if the browser is going to display the pdf inline, the button will say "view pdf" and when clicked will make the iframe visible

    • otherwise it will say "download pdf"

I found a way to detect wether the pdf has been loaded in the iframe: reading iframe.contentDocument.contentType after onload has fired, but

  • this won't allow me to display the correct button
  • onload does not fire if the file is being downloaded

Thanks :)

A: 

You could send a HEAD request to that resource and check what Content-Type and Content-Disposition values are being sent back. Based on these information you could determine whether a browser would display or download that resource.

Gumbo
Only if you assume that the browser will have a PDF displaying plugin installed and configured to be used by default. It find such things so irritating that I make a point of uninstalling or blocking them.
David Dorward
To the best of my knowledge, Content-Type and Content-Dispositionare sent from the server to the client. The server won't have any knowlwdge about browser capabilities. The server can exploit those two headers to force the browser to perform a download instead of a display.
Matteo Caprari
+3  A: 

To tell the client's browser to download a response as a file, set the Content-Disposition HTTP header to 'attachment' in your response. This is no guarantee, but it's the proper method of telling the browser how to handle the content.

Jeff Ober