Thursday, August 14, 2008

Download File Script for ASP

Well after tons of frustration with ASP (which sucks in my opinion), I was finally able to find a way to download files properly. So, if you every wanted to make it so your links aren't straight to the files, but to an intermediate page first, here you go. In PHP this is much simpler, FYI...

<%
Response.Buffer = True
Dim strAbsFile
Dim strFileExtension
Dim objFSO
Dim objFile
Dim objStream
'Set this to the variable in your GET
Set filename = request.QueryString("wp")
'-- set absolute file location
strAbsFile = Server.MapPath(filename)
'-- create FSO object to check if file exists and get properties
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
'-- check to see if the file exists
If objFSO.FileExists(strAbsFile) Then
Set objFile = objFSO.GetFile(strAbsFile)
'-- first clear the response, and then set the appropriate headers
Response.Clear
'-- the filename you give it will be the one that is shown
' to the users by default when they save
Response.AddHeader "Content-Disposition", "attachment; filename=" & objFile.Name
Response.AddHeader "Content-Length", objFile.Size
Response.ContentType = "application/octet-stream"

Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
'-- set as binary
objStream.Type = 1
Response.CharSet = "UTF-8"
'-- load into the stream the file
objStream.LoadFromFile(strAbsFile)
'-- send the stream in the response
Response.BinaryWrite(objStream.Read)
objStream.Close
Set objStream = Nothing
Set objFile = Nothing
Else 'objFSO.FileExists(strAbsFile)
Response.Clear
Response.Write("Le fichier est inexistant.")
End If
Set objFSO = Nothing
%>


P.S. I hacked this out from some download manager but don't remember it to give proper credit (sorry).

No comments: