...

View Full Version : Download file instead of navigate to URL



Leon
12-15-2002, 05:05 PM
Is it possible to create link that will start file download ("Save target as...") instead of navigate to URL?

Thanks in advance for any suggestions.

joh6nn
12-15-2002, 05:48 PM
i think it's possible in IE, but not cross browser. the best way to make it happen cross browser, is to zip it, as most browsers are set to automatically download zip files.

Leon
12-15-2002, 05:56 PM
Thanks.

The best solutions I come with are:
to use ExecWB of IE or to zip target file.

Still may be there is some other trick out there?

brothercake
12-15-2002, 06:16 PM
There is a method in IE called execCommand; it goes something like

document.execCommand("saveas","filename");

MSDN will have more ....

... but, it is IE only. The way to acheive this is server side. In PHP you make this file called "download.php":



<?

$saveName = stripslashes($HTTP_GET_VARS["name"]);
$savePath = stripslashes($HTTP_GET_VARS["path"]);

header ("Content-Type: application/octet-stream");
header ("Content-Disposition: attachment; filename=$saveName");
header ("Content-Transfer-Encoding: binary");
readfile($savePath);

?>


And then you call it with a link like this:



<a href="/download.php/?path=music/hifi/songname.mp3&name=songname.mp3">

Leon
12-15-2002, 07:49 PM
Thanks, brothercake, it work like magic.

Here mine interpretation in ASP for anyone interesting:

<%
Response.Write MapPath(Request.QueryString("Path").Item)
Response.AddHeader "CONTENT-TYPE", "application/octet-stream"
Response.AddHeader "CONTENT-DISPOSITION", "attachment; filename=" & Request.QueryString("Name").Item
Response.AddHeader "CONTENT-TRANSFER_ENCODING", "binary"
dim fso, f, ts
set fso = CeateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(MapPath(Request.QueryString("Path").Item))
Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault)
Response.Write ts.ReadAll
%>

You can call it with:
<a href="download.asp?Path=test.txt&Name=saveas.txt">Click here to download</a>

brothercake
12-15-2002, 08:11 PM
cool :)

joh6nn
12-15-2002, 08:53 PM
i'm gonna move this to the General Web Building forum. is this maybe one that we want to put in the archives?

krycek
12-15-2002, 08:57 PM
I would say yes, john - this question pops up so much etc. and this thread is a good answer :)

::] krycek [::

Skyzyx
12-18-2002, 12:21 AM
That covers the IE side of it. For Mozilla/Netscape, you shift-click the link to Save As...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum