0

I have created an iframe with some javascript to start download on page load after 5 seconds but it is not working for me nothing is happening and file is not downloading

<meta name="content-disposition" content="attachment; filename=http://dannykg.com/freebooks/4MindBlowing-Tips.pdf;">

<iframe id="download" width="1" height="1" style="display:none"></iframe> 
function startDownload () {         
    document.getElementById("download").src="http://dannykg.com/freebooks/4MindBlowing-Tips.pdf"; 
} 
setTimeout (startDownload, 5000);
Mark Alan
  • 435
  • 1
  • 5
  • 19
  • Possible duplicate of [Force pdf file download window from iframe](http://stackoverflow.com/questions/23727030/force-pdf-file-download-window-from-iframe) – CocoaBean Jul 24 '16 at 11:43
  • I am unable to understnd his code can you post in it to my answer to explain better as I am unable to ask question there – Mark Alan Jul 24 '16 at 11:48
  • Do you use any server-side language(PHP, ASP.net, JSP...)? – CocoaBean Jul 24 '16 at 11:51
  • javasript only it's html page – Mark Alan Jul 24 '16 at 11:53
  • thankyou.html only the above code is there on the page and a h1 heading – Mark Alan Jul 24 '16 at 11:54
  • Do you have to use the Iframe to download or is it ok if you have to use an other method? – CocoaBean Jul 24 '16 at 11:56
  • I would prefer with iframe as would be easy way isnt it or is there anything else – Mark Alan Jul 24 '16 at 11:57
  • Check the console output of your browser when you open this html file. Press F12 and go to CONSOLE. Ist here the error message "Resource interpreted as Document but transferred with MIME type application/pdf"? This would mean, that your server transfers the file differently. – konrad_pe Jul 24 '16 at 11:58
  • Check this answer too: http://stackoverflow.com/a/23013574 – konrad_pe Jul 24 '16 at 11:59

1 Answers1

0

Your code doesn't work because almost every browser today has got a built in PDF viewer, which means that your browser tries to display the PDF inside the invisible Iframe.

One way preventing this would be telling your server to use another content type for the PDF.

But there is a easier way, you just have to compress your PDF into a zip file. Then browsers will download it.

CocoaBean
  • 295
  • 7
  • 20