download link .asp (reply)

M

Mic

Couldn't figure out how to reply to a message this old, but thought
this could be handy (note... that was Michele, who is not me...
Michelle ;-)

I found this worked very well for the following purposes:
1) Forcing the download screen to pop up instead of previewing in
browser (achieved by leaving out the ContentType part)
2) Renaming the file... user prompted to save it under a name
different from that under which the file's stored on the server

Finally, I had to translate into JScript, so here's for the rest of
you who are unfortunate enough to use the language that nobody gives
examples in.

tmp_sql = "SELECT file_name_system, file_name_friendly ";
tmp_sql += "FROM files WHERE file_id = " + file_id;
tmp_rs = Conn.Execute(tmp_sql);

Response.Buffer = true;
var strFileName = tmp_rs("file_name_system");
var strFilePath = Server.MapPath("upload\\" + strFileName);
var fso = Server.CreateObject("Scripting.FileSystemObject");
var f = fso.getfile(strFilePath);
var strFileSize = f.size;
var adTypeBinary = 1;
Response.Clear;
var objStream = Server.CreateObject("ADODB.Stream");
objStream.Open;
objStream.Type = adTypeBinary;
objStream.LoadFromFile(strFilePath);
Response.AddHeader("Content-Disposition", "attachment; filename=" +
tmp_rs("file_name_friendly"));
Response.AddHeader("Content-Length", strFileSize);
Response.Charset = "UTF-8";
Response.BinaryWrite(objStream.Read);
Response.Flush;
objStream.Close;


....

Hi Michele,
yes there is but it takes a bit of code. First your links need to look
like
this
<a href="download.asp?f=doc1.doc">Download Doc 1</a>
<a href="download.asp?f=doc2.doc">Download Doc 2</a>
etc.....
now we need to create download.asp with this code, you can copy and
paste
it.
<%
Response.Buffer = True
strFileName=request("f")
strFilePath=server.mappath("/downloads/"&strFilename)
set fso=createobject("scripting.filesystemobject")
set f=fso.getfile(strfilepath)
strFileSize = f.size
set f=nothing: set fso=nothing
Const adTypeBinary = 1
Response.Clear
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = adTypeBinary
objStream.LoadFromFile strFilePath
strFileType = "image/jpeg" ' change to the correct content type for
your
file
Response.AddHeader "Content-Disposition", "attachment; filename=" &
strFileName
Response.AddHeader "Content-Length", strFileSize
Response.Charset = "UTF-8"
Response.ContentType = strFileType
Response.BinaryWrite objStream.Read
Response.Flush
objStream.Close
Set objStream = Nothing
%>

just change the line I've marked strFileType = to the correct content
type
for your downloads.

Jon
Microsoft MVP - FP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top