...

View Full Version : forcing download



ecnarongi
02-11-2003, 05:40 PM
If I use output.csv it forces the download but if I change the name to memorial.csv it doesn't. Can somebody please tell me how to force the download without naming everything output? Thank you, all help is appreciated.

Code_gate
02-11-2003, 08:42 PM
I'm not sure if this is what you're asking or not, but here is some code I have found useful:

<%
'--------------------------------------------
Response.Buffer = True
Dim strFilePath, strFileSize, strFileName

Const adTypeBinary = 1

strFilePath = "C:\ whatever the path is "
strFileSize = ... the size of file .. optional
strFileName = the file na,e

Response.Clear

'8*******************************8
' Requires MDAC 2.5 to be stable
' I recommend MDAC 2.6 or 2.7
'8*******************************8
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = adTypeBinary
objStream.LoadFromFile strFilePath

strFileType = lcase(Right(strFileName, 4))

' Feel Free to Add Your Own Content-Types Here
Select Case strFileType
Case ".asf"
ContentType = "video/x-ms-asf"
Case ".avi"
ContentType = "video/avi"
Case ".doc"
ContentType = "application/msword"
Case ".zip"
ContentType = "application/zip"
Case ".xls"
ContentType = "application/vnd.ms-excel"
Case ".gif"
ContentType = "image/gif"
Case ".jpg", "jpeg"
ContentType = "image/jpeg"
Case ".wav"
ContentType = "audio/wav"
Case ".mp3"
ContentType = "audio/mpeg3"
Case ".mpg", "mpeg"
ContentType = "video/mpeg"
Case ".rtf"
ContentType = "application/rtf"
Case ".htm", "html"
ContentType = "text/html"
Case ".asp"
ContentType = "text/asp"
Case Else
'Handle All Other Files
ContentType = "application/octet-stream"
End Select


Response.AddHeader "Content-Disposition", "attachment; filename= strFileName
Response.AddHeader "Content-Length", strFileSize
' In a Perfect World, Your Client would also have UTF-8 as the default
' In Their Browser
Response.Charset = "UTF-8"
Response.ContentType = ContentType

Response.BinaryWrite objStream.Read
Response.Flush

objStream.Close
Set objStream = Nothing

%>

ecnarongi
02-11-2003, 08:47 PM
Thanks, I think I figured out my problem.

whammy
02-12-2003, 12:51 AM
That looks like the same code I used, it was the first thing I found on a search of google, lol.

Works great, though.

reubenb
06-23-2004, 11:56 AM
(sorry a bit delayed here :eek: )
how would you make the above script have a query... i.e. download.asp?file=whatever.jpg ?
i tried doing it with request but it didn't work

it also gives me an error saying that the file could not be open. ?

?ta

whammy
06-23-2004, 05:19 PM
Hehe, one question at a time... first of all, to get the filename from the querystring you'd use:



<%
Dim filename
filename = Request.QueryString("file")
Response.Write("filename: " & filename & "<br />" & vbCrLf) 'To make sure you have the right value
%>


Regarding it not being able to open the file, make sure that you have the correct path (writing out the full path to debug... also you might want to look up Server.MapPath() on google) and that you have permissions to the directory the file is in.

Hope this helps. :)

BigDaddy
06-23-2004, 11:26 PM
I'm creating a file download page for our site....and I'm actually using a set of code very similar to what code_gate recommended. We're having an issue though, with me copyinig a file into the stream from the file server, before spitting it back out to the user.

For some reason, we can't get the rights set up correctly to allow the server access to the file system on the other server. It seems to me that if it can reference it on the same network, or by drive letter, it should work....but it won't.

reubenb
07-02-2004, 09:21 AM
Hi,
the below is my code and it doens't seem to work properly?
what am i doing wrong?
also.. how do i make it so if the file is not found it gives them a http status of 404?



<%
Response.Buffer = True
Dim strFilePath, strFileSize, strFileName, filename

Const adTypeBinary = 1

filename = Request.QueryString("filename")
strFilePath = "D:\Client Webs\xyz.com\" & filename
'strFileSize =
strFileName = filename

Response.Clear

Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = adTypeBinary
objStream.LoadFromFile strFilePath

strFileType = lcase(Right(strFileName, 4))

Select Case strFileType
Case ".asf"
ContentType = "video/x-ms-asf"
Case ".avi"
ContentType = "video/avi"
Case ".doc"
ContentType = "application/msword"
Case ".zip"
ContentType = "application/zip"
Case ".xls"
ContentType = "application/vnd.ms-excel"
Case ".gif"
ContentType = "image/gif"
Case ".jpg", "jpeg"
ContentType = "image/jpeg"
Case ".wav"
ContentType = "audio/wav"
Case ".mp3"
ContentType = "audio/mpeg3"
Case ".mpg", "mpeg"
ContentType = "video/mpeg"
Case ".rtf"
ContentType = "application/rtf"
Case ".htm", "html"
ContentType = "text/html"
Case ".asp"
ContentType = "text/asp"
Case Else
'Handle All Other Files
ContentType = "application/octet-stream"
End Select


Response.AddHeader "Content-Disposition", "attachment; filename= strFileName"
Response.AddHeader "Content-Length", strFileSize
Response.Charset = "UTF-8"
Response.ContentType = ContentType

Response.BinaryWrite objStream.Read
Response.Flush

objStream.Close
Set objStream = Nothing

%>

glenngv
07-02-2004, 02:31 PM
Try changing this line:

objStream.LoadFromFile strFilePath

to:


'enable error handling
on error resume next
objStream.LoadFromFile strFilePath
if err.number<>0 then
objStream.Close
Set objStrem=nothing
Reponse.Status="404 Not Found";
Response.End
end if
'disable error handling
on error goto 0

reubenb
07-03-2004, 09:52 AM
doesn't work at all now...

glenngv
07-05-2004, 03:14 AM
I had typos.


if err.number<>0 then
objStream.Close
Set objStream=nothing
Response.Status="404 Not Found"; 'remove the semicolon
Response.End
end if

But you said that the code doesn't work even without that modifications.

reubenb
07-05-2004, 03:23 AM
yeah.. just shows a blank page

BigDaddy
07-07-2004, 03:19 PM
Insert a debugging line inside your if err.number <> ""



response.write "boo"


If when running it, you see the word "boo", we know that it's finding an error and executing that bit of code.

ghell
07-07-2004, 05:43 PM
another way of forcing downloads can be done with fso but use the method said above unless you really cant for some reason.. fso needs some clever coding so that you do not memory out the server (close asap etc.. sometimes better done with dlls)

anyway.. you can prob ignore everything i just said as the other method is better :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum