PDA

View Full Version : Browser Force File Download Script Help


theflyingminstr
02-09-2008, 05:43 PM
Hi I have the following ASP script in a file named download.asp:

<%@Language="VBScript"%>
<%Option Explicit%>
<%Response.Buffer = True%>
<%
On Error Resume Next
Dim strPath
strPath = CStr(Request.QueryString("file"))
'-- do some basic error checking for the QueryString
If strPath = "" Then
Response.Clear
Response.Write("No file specified.")
Response.End
ElseIf InStr(strPath, "..") > 0 Then
Response.Clear
Response.Write("Illegal folder location.")
Response.End
ElseIf Len(strPath) > 1024 Then
Response.Clear
Response.Write("Folder path too long.")
Response.End
Else
Call DownloadFile(strPath)
End If

Private Sub DownloadFile(file)
'--declare variables
Dim strAbsFile
Dim strFileExtension
Dim objFSO
Dim objFile
Dim objStream
'-- set absolute file location
strAbsFile = Server.MapPath(file)
'-- 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("No such file exists.")
End If
Set objFSO = Nothing
End Sub
%>


When I call it to download a .jpg/.gif file it works fine, but when I use an .mp3 file it doesn't work.

Good: <a href="download.asp?file=/images/image.jpg">Download</a>

No Good: <a href="download.asp?file=/images/muisc.mp3">Download</a>


Any help much appreciated, thanks!


Mendi

Roelf
02-11-2008, 08:51 AM
i have done this a while ago, but then i did not use the charset property. That worked for images, .doc files, .zip files. Did not test for mp3 files, but i suggest to drop the UTF-8 for the charset

angst
02-11-2008, 04:20 PM
this is what I use:


Function DownloadFile(file)

Dim strAbsFile
Dim strFileExtension
Dim objFSO
Dim objFile
Dim objStream

strAbsFile = Server.MapPath(file)
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strAbsFile) Then

Set objFile = objFSO.GetFile(strAbsFile)
Response.Clear

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("There is no such file.")
End If

Set objFSO = Nothing
End Function