Click to See Complete Forum and Search --> : Forcing "SaveAs" dialog
whiter
03-08-2003, 09:03 PM
This question has been asked before all over the place, but I haven't been able to make use of the answers given.
More than one answer says use "document.execCommand('SaveAs')" or " document.execCommand('SaveAs',null,'filename.html')"
OK ?? BUT HOW???
How do I actually use this document.execCommand?
My requirement:-
I currently have an icon graphic which when clicked just goes to a PDF document:-
<a target="_blank" href="mydocument.pdf"> <img border="0" src="images/pdf_icon.gif" align="left" width="34" height="34"></a>
My requirement is that a left click of the mouse should bring up the SaveAs dialog, even if the MS IE browser has the PDF plug-in installed. How do I actually use 'document.execCommand' to do this?
Can someone give me a complete code snippet?
Many thanks
Richard
Originally posted by Dave Clark
At any rate... The real answer is either to ZIP your PDF document first
Or, use PHP to send the header information needed to force a download to the browser. Not exactly sure which browsers this works in, I've only tested in IE6.
whiter
03-09-2003, 06:55 AM
OK ..... so my next (obvious) question then is what is the server side code that I need, and where do I specify it?
Also, will this server side code affect ALL links? Or just certain specific links - which is what I am after.
I want to be able to help users, many of whom barely know what a mouse is, let alone the "complexities" of having to right-click or understanding how to unzip a file before pushing it into Acrobat Reader.
Originally posted by Dave Clark
I already said that:How the heck did I miss that? Sorry, Dave. I seriously didn't see that you already told whiter to use server side languages. I read the first part about zipping the file, and assumed that was your answer. My appologies. ;)
whiter
03-09-2003, 09:08 AM
Thanks for the details, unfortunately the host is a Unix one, and so we get Perl/PHP but not ASP.
Anybody got a PHP script that would do this?
Thanks,
Richard
Yup, here is some code for you...
Name this file filedownload.php...
<?PHP
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=$filename");
$fp = fopen($filename, 'rb');
fpassthru($fp);
fclose($fp);
?>
And you link to your files like this:
<a href="filedownload.php?filename=filetodownload.txt">Download this text file</a>
Originally posted by Dave Clark
Oh, I see... ...lazy man's coding.
Actually, I just wrote that up last night, and it wasn't complete. It needs to be as easy as possible for the user to add links. If you look at the difference between my link and your link, mine should be quite a bit easier for the average user to be able to use. I didn't want the users to have to specify a mime type.
Anyway, I should have added the content-length before I posted. Here that is:
<?PHP
header("Content-type: application/octet-stream");
header("Content-Length: ".filesize($filename));
header("Content-Disposition: attachment; filename=$filename");
$fp = fopen($filename, 'rb');
fpassthru($fp);
fclose($fp);
?>
whiter
03-09-2003, 12:42 PM
Thanks for the PHP code - guess I'll have to learn that :cool:
As you said - works and is simple to use.