kkohl
asked on
Force a pdf file to download instead of opening.
Have a special case instance where I would like a visitor to be able to download a pdf form without the pdf file first opening in a new window.
What I have working:
straight html page (frames).
a link to name.pdf.
Using MS Frontpage
My only options for the pdf hyperlink were to have it open in the same page, specific frame, or new window.
And what seems to be a real puzzle is that I have security set on the pdf document to allow the filling in of form data, printing and saving, but nothing else.
If the user opens up the pdf in a new window and fills in any information on the form, it is saved to the pdf regardless of being on the web server??? the next user can open up the pdf and any information previously filled in, is right there still visible.
So, for my purposes with this pdf form, I need to force download instead of opening as a web page.
TIA,
kkohl
What I have working:
straight html page (frames).
a link to name.pdf.
Using MS Frontpage
My only options for the pdf hyperlink were to have it open in the same page, specific frame, or new window.
And what seems to be a real puzzle is that I have security set on the pdf document to allow the filling in of form data, printing and saving, but nothing else.
If the user opens up the pdf in a new window and fills in any information on the form, it is saved to the pdf regardless of being on the web server??? the next user can open up the pdf and any information previously filled in, is right there still visible.
So, for my purposes with this pdf form, I need to force download instead of opening as a web page.
TIA,
kkohl
Download a file instead of opening it. IE only and assuming you have ASP capabilities:
<%@Language="JScript"%>
<%
var myFSO = new ActiveXObject("Scripting.F ileSystemO bject")
var objFile = myFSO.openTextFile(Server. MapPath("/ path/somef ile.pdf")) ;
var str = new String(objFile.readAll());
Response.ContentType = 'application/unknown';
Response.addHeader('conten t-disposit ion', 'attachment;filename=somef ile.pdf');
Response.Write(str);
%>
Cd&
<%@Language="JScript"%>
<%
var myFSO = new ActiveXObject("Scripting.F
var objFile = myFSO.openTextFile(Server.
var str = new String(objFile.readAll());
Response.ContentType = 'application/unknown';
Response.addHeader('conten
Response.Write(str);
%>
Cd&
I've come to the conclution that the best method is to just ask the users to right click the link and choose "save as". Of course that's not what you are looking for so I will suggest you use CobolDinosaur's method, or it's equivalent in any other server side scripting language like PHP, .NET, even Perl.
I don't think this can be done on the client side, check this link to see what I mean (and for code on how to do it in every server side language):
https://www.experts-exchange.com/questions/21073694/Hyperlink-how-to-avoid-playing-the-media-file-directly-and-open-a-File-Download-dialog.html
Best regards,
Esopo.
I don't think this can be done on the client side, check this link to see what I mean (and for code on how to do it in every server side language):
https://www.experts-exchange.com/questions/21073694/Hyperlink-how-to-avoid-playing-the-media-file-directly-and-open-a-File-Download-dialog.html
Best regards,
Esopo.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Why don't you just put the .pdf file into a .zip file?, .zip files can't be opened by the browser, so each time the user wants to download one he'll have to save it to the drive, no matter what...
Hope that works for you...
;)
Hope that works for you...
;)
I've come to the conclution that the best method is to just ask the users to right click the link and choose "save as". Of course that's not what you are looking for so I will suggest you use CobolDinosaur's method, or it's equivalent in any other server side scripting language like PHP, .NET, even Perl.
I don't think this can be done on the client side, check this link to see what I mean (and for code on how to do it in every server side language):
I don't think this can be done on the client side, check this link to see what I mean (and for code on how to do it in every server side language):
ASKER
It's all in the headers, as Zyloch pointed out. I did find a little clearer answer when digging a little deeper on the web.
I changed the file extension from .pdf to .fdf, and added the headers to account for the downloading of my file.
<meta http-equiv="Content-Type" content="application/vnd.f df; name="%s.FDF"">
<meta http-equiv="Content-Dispos ition" content="attachment; filename="%s.FDF"">
The user can still choose to open the file from the current location but it is not automatic and the default button that is focused is the save button. Also, it was verified that if the user opens the file from the site location, any form changes are not saved to that web location regardless if the user tries to or not.
This method answered all requirements.
I changed the file extension from .pdf to .fdf, and added the headers to account for the downloading of my file.
<meta http-equiv="Content-Type" content="application/vnd.f
<meta http-equiv="Content-Dispos
The user can still choose to open the file from the current location but it is not automatic and the default button that is focused is the save button. Also, it was verified that if the user opens the file from the site location, any form changes are not saved to that web location regardless if the user tries to or not.
This method answered all requirements.
ASKER
(yes, this method was the fix for IE, there are other headers for the different browsers)
Very clunky way if you ask me, but it was done in straight HTML, no asp or xml, no java, no perl... and the bottom line is, it works.
Thanks all!
Very clunky way if you ask me, but it was done in straight HTML, no asp or xml, no java, no perl... and the bottom line is, it works.
Thanks all!
Hi There,
Here is what I would use...
<?php
header('Content-Type: application/save');
header('Content-Dispositio n: attachment; filename=<<<FILE NAME>>');
readfile(<<< ABSOLUTE PATH TO YOUR FILE >>>);
?>
Hope it helps
Matt Wilkes
Here is what I would use...
<?php
header('Content-Type: application/save');
header('Content-Dispositio
readfile(<<< ABSOLUTE PATH TO YOUR FILE >>>);
?>
Hope it helps
Matt Wilkes
Matt Wilkes is right you can force to download a file without displaying but adding specific headers.
Debjit Kar
Debjit Kar
The only other way to do this without a server-side scripting language is to zip up the pdf file.
Regards,
${Zyloch}