Forum Moderators: open

Message Too Old, No Replies

Automatically Start Download

         

jini11

5:44 pm on Mar 30, 2005 (gmt 0)



Is there a Javascript/HTML way to automatically trigger download of a file from a html page?

rocknbil

6:03 pm on Mar 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<html>
<head>
<TITLE>Download</TITLE>
<meta http-equiv=REFRESH CONTENT="5; url=http://path_to/your/download.zip">
</head>
<body>
Your download should begin in five seconds. If it does not, please <a href="http://path_to/your/download.zip">click here</a>.
</body></html>

This will only work for types that are NOT recognized by the browser. If it's a recognized type (html, jpg, etc.) it will just display. To to that you have to use server-side programming and "munge" the header. For example, let's say you want to force the DOWNLOAD a .gif file, which would normally display. So you print to the browser:

(Use some process to open the file and store it in memory - this is a perl snippet)

$fname = 'downloaded.gif';
open (FILE, $myfile);
$imagefile = <FILE>;
close (FILE);

print "content-type: bad/type\n";
print "Content-Disposition: attachment; filename=$fname\n\n"; #<-- populates file download dialog
print "$imagefile";

"Bad/Type" can be anything: ASfDedG - it just has to send to the browser an unrecognized MIME content-type header. That's what forces the file download.