Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hyperlinking

Status
Not open for further replies.

pcs800

IS-IT--Management
Apr 9, 2002
339
US
Can I create a hyperlink to a .doc or .pdf that, when clicked, would bring up the save dialog box instead of launching the file.

Eric VanLandingham
The Bargain Monkey
 
You can enclose it in a zip file, and that will initiate the "Save" dialog box. It can be self extracting or a regular zip file, but with a regular zip file, of course, the person will need to have WinZip or some other type of zip software installed to be able to open it.
 
yes, that's the obvious choice, I am looking for a way to initiate the download as the hyperlink is clicked without changing the extension.

Eric VanLandingham
The Bargain Monkey
 
You can try this javascript function, although I haven't used it so I don't know about cross-browser or backward compatibility:

<a href="yourfile.doc" onClick="document.execCommand('Save As')">Your Word or PDF File</a>


 
Damn, I thought for sure that would work. I have IE6 and it doesn't work. It just opens the pdf in acrobat reader within the browser like it did before with a plain link.
Anything else with that code that might help?

Eric VanLandingham
The Bargain Monkey
 
here is what i have

<a href="test.pdf" onClick="test.execCommand('Save As')">Test PDF File</a>

is that right?
Do I need to have <javascript> before it or something?
HINT: I am not much of a code master

Eric VanLandingham
The Bargain Monkey
 
you need

<a href="test.pdf" onClick="document.execCommand('Save As')">Test PDF File</a>

(Document refers to the current document, not the name of the doucment.) ;-)
 
Yes, I'm using IE6 as well, plus I tested it in Netscape 7.02 and Netscape 4.08 and it worked in all of them.
 
Oops, it looks like it works with Word documents, but not PDF files. I guess I hadn't tested it with an actual pdf file until just now and the pdf file opened in Acrobat Reader for me too.
 
the .doc opens in my browser as well. Niether link does what I wanted it to.

Do you understand this below? I don't use htaccess, any way to do it with javascript or something?

Don't tell the browser what the file is:
The http header tells the user agent what the file contains, this allows it to take a suitable action. You can claim not to know what the file is, and most browsers will default to prompting the user to save it.

You can add these lines to your .htaccess (Or use some other technique to send a application/octet-stream header):


<FilesMatch "\.(doc)$" >
ForceType application/octet-stream
</FilesMatch>

The above tells the server to say "This is some bytes, I don't know what meaning they have" instead of "This is an MS Word document". This just makes it harder for browsers to do what the user wants with the file.


Eric VanLandingham
The Bargain Monkey
 
It's possible that the only other option is to instruct your viewers to right click and do a "Save Target As". Even with the Word document, they still have the option to open it from the "File Download" dialog box rather than saving it.

You might want to ask over in the javascript forum too, as maybe someone there has another option:

 
I guess we were posting at the same time.

There very well might be a way using .htaccess, but I don't know about it.

It did work for me with Word files though, but again, you might want to post over in the javascript forum and also if there's another forum that might deal more with .htaccess files.

Javascript depends on the user having it enabled too, which isn't anywhere near 100% so not the best option anyway.
 

If you have acces to the server, configure the MIME type for .doc and .pdf to be some generic binary file. Then users should always get the Save box.

Hope this helps,
Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top