faqts : Computers : Programming : Languages : JavaScript : Document

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

236 of 340 people (69%) answered Yes
Recently 10 of 10 people (100%) answered Yes

Entry

How can I force a File Download of a .pdf file with a hyperlink (bypassing Acrobat Reader)?

Jan 26th, 2006 01:44
Andre White, Ian Vink, Justin Pasher, Arjan Kemeling, Darton Williams, Joe Lipman, Eilon Reshef, Martin Honnen, Kim Denz,


Client side JavaScript can't achieve that. It is best to teach your 
users to use the existing browser functionality, that is right click-
>save as in the context menu.
[Added here]
You can use the content-disposition HTTP field on the server side.
[another added]
Sorry to contradict the above but late versions of Internet Explorer 
allow a client-side save via the dHTML method 
    document.execCommand("SaveAs")
Although this only saves the current webpage, you could open a 
seperate 
window (via javascript) which temporarily opens the file, executes the 
save function, then closes again. so...
    myTempWindow = window.open("thePdfFile.pdf","SaysaMe");
    myTempWindow.document.execCommand("SaveAs",null,"SaveAsName.pdf");
    myTempWindow.close();
To be honest, I don't know what the 'null' does as it seems to work 
the 
same if you replace with 'true' or 'false'.  If you get an error, try 
replacing the 'null' with these until it works.
[Still more added]
After going around and around with a client over this issue, I finally 
told the client to zip all of his pdf's (and pdf's compress very well, 
by the way) and no more problems. I tried everything under the sun, 
including ColdFusion CFCONTENT, Javascript, content-disposition, etc., 
and still couldn't get pdf's to reliably save to disk cross-browser. 
If 
you absolutely MUST use bare pdf files, then I agree with the first 
answer here. In fact, you could place some instructions right on the 
page telling the user how to right-click and save as.
[The latest addition so far]
I have taken the above script and tweaked it a bit.
Now it doesn't show you the window that opened (it's position is off 
screen) and you can use the same script for multiple downloads.
Notice that I replaced the double quotes with single quotes.
I have posted it on my own site where you can try it out:
http://www.whitewater.be/resources/javascript/view.asp?
action=view&id=48
And this is the code :
<html>
<body>
<script>
 function downloadme(x){
    myTempWindow = window.open(x,’’,’left=10000,screenX=10000’);
    myTempWindow.document.execCommand(’SaveAs’,’null’,x);
    myTempWindow.close();
}
</script>
<a href=javascript:downloadme("/test.pdf");>Download this pdf</a>
</body>
</html>
Arjan Kemeling
[Life CAN be simple]
After having spent some time finding a way to by-pass the file 
association problem for PDF's I just renamed the PDF file !
By removing the file extention '.pdf' you cause your browser to ask 
you 
what you want to do with the unknown file.
When saving it, it does appearently read the file and recognize it as 
a 
PDF. This will cause the Save As dialog box to save it with PDF file 
extention and so you just need to hit the OK button !
You can test it on my website :
http://www.whitewater.be/resources/javascript/view.asp?
action=view&id=78
Arjan Kemeling
[New Comment - 6-18-2003]
Using the Content-Disposition header correctly should force IE and 
other
browsers to treat the file as an attachment/download. I do it all the
time. Make sure you specify attachment as the type. ex:
Content-Disposition: attachment; filename="file.pdf"
Obviously this is easiest if you are dynamically outputing the contents
of the PDF from a server script script (like Perl, PHP, ASP, etc).
IAN VINK
--------------------
in C# with .NET you can do it like this:
string downloadPath ="c:\blah blah blah";
Response.ContentType = "APPLICATION/OCTET-STREAM";
Response.AddHeader( "Content-Disposition", "attachment;filename=\"" + 
downloadPath + "\"" );
StreamWriter w = new StreamWriter(Response.OutputStream);
w.Write(xml);
[New Comment - 01-26-2006]
Andre White
Content-Disposition seems to work well (i.e. C# example above)
However, MSIE seems to ignore this information and sniff to guess the
true filetype.  Although I've found no official documentation from MS,
it appears that MSIE fails to 'guess' the correct type when running IIS
6.0 with compression enabled.  Mozilla / Opera properly use the Header
information supplied.  I have yet to find a work around.