Forum Moderators: open

Message Too Old, No Replies

link to download, not open, a pdf file

Click here to download file

         

Chair

4:51 pm on Jan 24, 2007 (gmt 0)

10+ Year Member



My client produces a .pdf format and he wants people to download it from his web site. (link to download)
How can you do this? Is there a way of specifying in the link that it should go straight to the file download procedure or use a javascript? "Click here or click this link to download file" will ask visitor and have this work for PC's and Macs. Any ideas? Hope to hear from you .Thank you.

Terabytes

5:01 pm on Jan 24, 2007 (gmt 0)

10+ Year Member



If the users computer has Adobe reader installed...the default action on click is to open the document in adobe reader....

If the user does not have adobe reader installed...the click will result in a "save as..." dialog box...

you can possibly prompt your users to right-click the link and "save as..." to download the file...

or....go ahead and open the file in adobe reader then FILE /SAVE A COPY to get the .pdf to their desktop (or wherever...)

hope that helps!
Tera

rocknbil

8:33 pm on Jan 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can try simply instructing users to right-click - but people don't like to read. :-D

Here is the problem: browsers associate files with helper applications. So if directly linked to, the browser will always recognize a PDF (or a .gif, .jpg, etc.) and display it in the browser using a plug in, or prompt you to install the plug in. In some browsers this is true even if you change the extension - it "sniffs out" the file type by reading in the file headers.

So if you want to force a download of a known file type, I know of only one way to do it: create a server-side application that supplies an unknown content-type.

When you use an application to output to the browser (perl, PHP, asp, etc.) you need to sent the mime type to the CGI gateway or it will generate an error. Normally this is

content-type: text/html

or for PDF,

content-type: application/pdf
or
content-type: application/octet-stream

When you send a bad content-type, the browser doesn't know what to do with the file. So it prompts for a download. You can make this really slick by pre-populating the filename box. This is especially cool in online purchases, where the actual file is stored in a hidden location. You can supply a PDF file called 123456.hid and send it as user_manual.pdf.

So if you get anything I've said above, here is a perl example on how to do this. Note "bad/type." That can be anything except a known mime type, as "herbs/widgets" or "sgfdfsdf/dfsd".

$CRLF = "\x0d\x0a"; #define octal line feed
$file = '/full/path/to/123456.hid';
$filename = 'manual.pdf';
# Get the length of the content, it must be file length + 1
if (-e $file) { $size = -s $file; $size++; }

Print to browser.
print "Content-type: bad/type$CRLF";
print "Content-Length: $size$CRLF";
print "Content-Disposition:attachment;filename=$filename$CRLF$CRLF"; ## Two newlines

open (FILE, "$file_loc") or &some_error_routine(!$);
while (<FILE>) { print; }
close (FILE);

That's it, it will download.

sldesigns

8:22 pm on Jan 26, 2007 (gmt 0)

10+ Year Member



You could also force the pdf to open in a new window -- while the pdf might display, at least it doesn't interfere with navigation. ONE NOTE-- when you list the name/link, please put on the file size!

Over at 456Bereastreet there is javascript code for this, so you need to use target:blank.

[456bereastreet.com...]