/    Sign up×
Community /Pin to ProfileBookmark

Forcing "SaveAs" dialog

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

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@pyroMar 09.2003 — [i]Originally posted by Dave Clark [/i]

[B]At any rate... The real answer is either to ZIP your PDF document first[/B][/QUOTE]

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.
Copy linkTweet thisAlerts:
@whiterauthorMar 09.2003 — 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.
Copy linkTweet thisAlerts:
@pyroMar 09.2003 — [i]Originally posted by Dave Clark [/i]

[B]I already said that:[/B][/QUOTE]
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. ?
Copy linkTweet thisAlerts:
@whiterauthorMar 09.2003 — 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
Copy linkTweet thisAlerts:
@pyroMar 09.2003 — Yup, here is some code for you...

Name this file filedownload.php...
[code=php]<?PHP
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=$filename");
$fp = fopen($filename, 'rb');
fpassthru($fp);
fclose($fp);
?>[/code]


And you link to your files like this:

&lt;a href="filedownload.php?filename=filetodownload.txt"&gt;Download this text file&lt;/a&gt;
Copy linkTweet thisAlerts:
@pyroMar 09.2003 — [i]Originally posted by Dave Clark [/i]

[B]Oh, I see... ...lazy man's coding.[/B][/QUOTE]

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:

[code=php]<?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);
?>[/code]
Copy linkTweet thisAlerts:
@whiterauthorMar 09.2003 — Thanks for the PHP code - guess I'll have to learn that ?

As you said - works and is simple to use.
×

Success!

Help @whiter spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...