1

Possible Duplicate:
jquery: how to force a pdf download automatically?

Afternoon folks,

I have a pdf file which the user can download. I want this to be done from Javascript rather than an <a> or similar.

So I have this code, but it opens in a new window/tab.

  window.open(url,'Download');

How can I tell the browser to show a download dialog box, instead of displaying the PDF inline?

Community
  • 1
  • 1
Matt
  • 3,664
  • 3
  • 33
  • 39
  • Don't do this; it is user-unfriendly. If I told my browser to open PDF files instead of download them, that's what I want!! No website should try to override it. – Domenic Oct 07 '11 at 14:25
  • @Domenic - it is a link to download the pdf though. If I click a link to download something I dont want it opened for me! – Matt Oct 07 '11 at 14:33
  • If I click on a link to a PDF on the web, I always want to view the PDF, never to download it. That's why I set up my browser that way. – Domenic Oct 07 '11 at 14:36

1 Answers1

2

Your server has to serve up the document with a "Content-disposition" header:

Content-disposition: attachment; filename="yourfile.pdf";

You can't make that happen from JavaScript.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • Yes its straight forward if I set it server side, sadly I want to do it client side – Matt Oct 07 '11 at 14:32
  • Well I don't think there's any way to force that. That is, I don't know of any way to tell a browser to perform an HTTP operation while also forcing it to behave as if the server did send back a "Content-disposition" like that. – Pointy Oct 07 '11 at 14:36
  • No same. I didn't know if there was a way to pass the mime type to `window.open`. – Matt Oct 07 '11 at 14:38
  • I modified it to work like this in the end. – Matt Nov 13 '11 at 11:44