Forum Moderators: open

Message Too Old, No Replies

PDF Download via a button rather than Right Click - Save As

         

SimonHarrison

6:05 pm on Jan 24, 2006 (gmt 0)

10+ Year Member



We have for years had our members use the old

'Right Click and Choose Save As ' method for getting their PDF's

However today I purchased an ebook that gave me a login to a system that had a simple 'button' that allowed me to click on it and up popped the same download box.

I've taken a screen shot of it here: snip

Does anybody know how this is done or could direct me to a script?

[edited by: encyclo at 6:08 pm (utc) on Jan. 24, 2006]
[edit reason] no links to screenshots please [/edit]

Tapolyai

6:11 pm on Jan 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can you describe it exactly how you want it to work? Is this a PDF file that is created on the fly?

A simple anchor link to a PDF file will either open the PDF, or pop-up the download box - depending on the browser's setting.

encyclo

6:14 pm on Jan 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need server-side scripting or the ability to alter mime types with .htaccess or similar. Here are a couple of threads which might help:

[webmasterworld.com...]
[webmasterworld.com...] (msg#3)

SimonHarrison

6:29 pm on Jan 24, 2006 (gmt 0)

10+ Year Member



Thanks encyclo I will give the code below to my developer see what he can do

Tapolyai, no it's not created on the fly, nothing that complicated, just a pdf we upload to a site that we woudl like users to be able to download just by clicking a download button rather than fart around with right click save as etc.

I did post a screen shot of exactly what it was doing but it's been removed , not quite sure why that is I guess somebody must have posted something nasty in a screen shot.

I will paste this in for anybody who does a search in the future on PDF and downloading via a button

What you need to do is "tell" the browser it's something else from the server side and munge up the content-type header - that is, give it a content-type you KNOW it can't possibly understand. A sample in perl:

## Store the filename in a variable:
$filename = 'yourfile.mov';

## open file and store in a variable (e.g., in memory)
open (FILE,"$filename");

## Not usually, but SOMETIMES, on some servers, you
## have to specify binary mode when reading the file.
## binmode FILE;

while (<FILE>) { $file .= $_; }
close (FILE);

## Can't remember why this must be one more byte than length!
$size = length( $file)+1;

## tell the browser it's an unknown type.
## this can be anything that is NOT a known
## type, ex "Content-type: blabla/debla";
print "content-type: bad/type\n"; # ONE return

print "Content-Length: $size\n"; # ONE return

# "filename" populates save box.
print "Content-Disposition: attachment; filename=$filename\n\n"; ## TWO returns flushes the content-type header and ...

print " $file"; ## deliver the goods.

This works for binary or ASCII data; pdf's, media, images, anything. When you munge the content header the browser's only option is to offer the requested file as a download. The above can be emulated in any server-side language. But I don't know what you can do in JS, never tried. :-)