ODBiC Discussion Board


RE: Force download, Phil, 02-22-2006

Use the HEADER command and output the file from the script

by Roger Harris, February 22, 2006 10:19

When you link directly to a file and that URL comes back into the server, the server will automatically send back a Content-Type header depending on the MIME type of the file extension. It's that Content-Type header that will cause the browser to invoke a particular application to handle the file, if you have one defined on your system, or if not, then to open the standard "Save as..."/"Open with..." download window. So, to force a download of a file type that's usually opened by an application, you need to send a "generic" Content-Type header such as "application/octet-stream". You should also send a Content-Disposition header to say that an "attachment" is being sent, and to provide the file name. It's also a good idea to send a "Connection: close" header so the browser will not attempt to send any more requests on that connection after the file is downloaded. The set of response headers need to be terminated by a blank line, which can be output with an empty HEADER "" command.

Then you can INCLUDE the file in the script, which will send it out to the browser. But, for anything other than text files, you need to set the output mode to "binary", which can be done with the SETOPTION output_mode=B command.

IMPORTANT: You need to be very careful that the HEADER statements and the file INCLUDE are the only output produced by the script, so it's a good idea to follow the INCLUDE with an EXIT.

Assuming that you have the name of the file in a variable named $File_Name$ and the local file system path in $File_Path$, then this should work:

<%
HEADER "Content-type: application/octet-stream";
HEADER "Content-Disposition: attachment; filename=$File_Name$";
HEADER "Connection: close";
HEADER "";
SETOPTION output_mode=B;
INCLUDE $File_Path$/$File_Name$;
EXIT;
%>


Replies:


Post Your Reply:

Name   
E-mail  optional
Topic  
Message:

HTTP Link: 
Link text: 

          


Copyright ©1997-2003, Roger Harris. All rights reserved.