Topic: How do you make a link to cause a download (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=28099" title="Pages that link to Topic: How do you make a link to cause a download (Page 1 of 1)" rel="nofollow" >Topic: How do you make a link to cause a download <span class="small">(Page 1 of 1)</span>\

 
Morph
Paranoid (IV) Inmate

From: The Soft Cell
Insane since: Nov 2001

posted posted 06-20-2006 21:01

I have a few things on my personal web space that I want other people to be able to download, eg a pdf or mp3 file.

if I just type the web address of these files then the pdf will open or the mp3 will play, so how can I get them to download instead so the user has the option to run or save them...?

poi
Paranoid (IV) Inmate

From: Norway
Insane since: Jun 2002

posted posted 06-20-2006 21:20

Have the server return those files with a Content-Type:application/octet-stream header and the user agent will prompt the user to save them.

Hope that helps,

DL-44
Lunatic (VI) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 06-20-2006 21:49

Of course, just for the record, the user *always* has the option of whether to open or save a link (via the right-click menu...)

Morph
Paranoid (IV) Inmate

From: The Soft Cell
Insane since: Nov 2001

posted posted 06-20-2006 21:49

hi poi, thanks for the reply

way over my head though.

Morph
Paranoid (IV) Inmate

From: The Soft Cell
Insane since: Nov 2001

posted posted 06-20-2006 22:58

if I purchase an ebook for example from ebay, the seller sends me an email with a link. I click the link and the download window opens. That is what I'm trying to do

Suho1004
Maniac (V) Mad Librarian

From: Seoul, Korea
Insane since: Apr 2002

posted posted 06-21-2006 09:04

To clarify what poi said (maybe), if you are using PHP you can take a look at the manual page on header for more information. If you are not using PHP or another server-side scripting language, it is not possible to tell the server to return files with a certain header type. Instead, you could just go with the old "right-click to save" text (as DL mentioned). Honestly, I think the latter is your best option here. Give the user the choice.


___________________________
Suho: www.liminality.org | Cell 270 | Sig Rotator | the Fellowship of Sup

Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 06-21-2006 10:05

Actually, Suho, if you were using Apache, you probably could tell which header to send via .htaccess.

Morph
Paranoid (IV) Inmate

From: The Soft Cell
Insane since: Nov 2001

posted posted 06-21-2006 11:19

this is more complex than I expected. I am aware that if I zip the file before uploading then it will download as I want but I was hoping to avoid zipping

poi
Paranoid (IV) Inmate

From: Norway
Insane since: Jun 2002

posted posted 06-21-2006 12:41

sorry. Well the insights/options given by the other inmates are good.

If you can't/don't want to get your hands on the server, a "right-click to save" note should be enough.

Tyberius Prime
Maniac (V) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 06-21-2006 13:10

Back to my .htaccess variant,

try placing a file called .htaccess with the following code in the directory with your pdfs.

code:
AddType application/octet-stream pdf



(or possibly)

code:
<Files *.pdf>
ForceType application/octet-stream
Header set Content-Disposition attachment
</Files>



That should work - but IE apperantly is a second guessing son of a... (don't get me started on IE and caching and the headers involved...)

Suho1004
Maniac (V) Mad Librarian

From: Seoul, Korea
Insane since: Apr 2002

posted posted 06-21-2006 13:52

TP: good point, .htaccess would work on apache, but unless he wants all of a certain type of file to prompt a download, it might get sticky with a lot of different rules.

I still hold that just letting the user right-click is the cleanest way to go. But that's just my opinion.


___________________________
Suho: www.liminality.org | Cell 270 | Sig Rotator | the Fellowship of Sup

H][RO
Bipolar (III) Inmate

From: Australia
Insane since: Oct 2002

posted posted 06-21-2006 17:53

While the right click to save thing is fine, some times you want certain functionality - and alot of users dont actually use the right click... There is nothing wrong with a download box since it still gives you the option to open or save.

I have used this php code for a while and it seems to work fine.

code:
$fileName = 'myfile';
$fileExtension = 'pdf';

$sourceFile = $fileName . ' . ' . $fileExtension;
$outputFile = $sourceFile; // the name they save as can be different to the existing file

// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off');

 switch( $fileExtension)
 {
    case "pdf": $ctype="application/pdf"; break;
    case "exe": $ctype="application/octet-stream"; break;
    case "zip": $ctype="application/zip"; break;
    case "doc": $ctype="application/msword"; break;
    case "xls": $ctype="application/vnd.ms-excel"; break;
    case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
    case "gif": $ctype="image/gif"; break;
    case "png": $ctype="image/png"; break;
    case "jpeg":
    case "jpg": $ctype="image/jpg"; break;
    default: $ctype="application/force-download";
}

session_cache_limiter("");

header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=".basename($outputFile).";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($sourceFile));


@readfile("$sourceFile") ;  // This is the bit that prompts for the download, supress errors for niceness
exit();



(Edited by H][RO on 06-21-2006 17:55)

Suho1004
Maniac (V) Mad Librarian

From: Seoul, Korea
Insane since: Apr 2002

posted posted 06-22-2006 03:54

Great example code, but I'm just getting the feeling that Morph might not be using PHP, and (even if he is) that he was looking for something simple (like something you can just add to a link). In terms of simplicity, reminding users that they can use the context menu to download is probably the way to go.


___________________________
Suho: www.liminality.org | Cell 270 | Sig Rotator | the Fellowship of Sup

DL-44
Lunatic (VI) Inmate

From: under the bed
Insane since: Feb 2000

posted posted 06-22-2006 05:23

^ I heartily agree, Suho.

H][RO
Bipolar (III) Inmate

From: Australia
Insane since: Oct 2002

posted posted 06-22-2006 06:21

Yeh well it entirely depends on what he wants to do, if he is using php could just throw that code in a download.php file then have some (securely checked) get/post variable to determine the file to download.


The main reason I use it is to track downloads, and perform login security checks on downloads etc.

If its super basic then yeh just do the right click save as, put a message if you feel the need.


p.s if you are on a windows server depending on your control panel you might be able to set the 'MIME Types' and add file extensions which will allow people to download it. *From memory* doing this actually displays the prompt - not 100% sure.

horsedreamer
Bipolar (III) Inmate

From: The Leather Wheeliechair
Insane since: Dec 2004

posted posted 02-13-2007 08:45

well he may not be using it, but I'll snatch that sample code if you don't mind and put it to some use.

---.sig-----------------------------------------
I have not fallen from grace; Grace has fallen from me.
"If I close my mind in fear, please pry it open"
- Metallica "Outlaw Torn"



Post Reply
 
Your User Name:
Your Password:
Login Options:
 
Your Text:
Loading...
Options:


« BackwardsOnwards »

Show Forum Drop Down Menu