Hello,
I’m pretty new to ASP, only been doing it for about six months. I apologize for the long post, but I’ve been working on this one script for four days to no avail.
I’m working on a small e-commerce site that allows users to pay for audio files (mp3s), create an account, then download the audio files. I’ve set up a download script (which came from a user on another forum) that securely finds the files and then “forces” the download of these files. I’ve added read and write permissions to the folder they’re stored in via my webhost’s control panel.
What’s happening is when I click on the HTML link I’ve set up
<a href=“downloadScript.asp?file=5-5-06a_TimGatten.mp3&force=true”>Download</a>
the download dialog box comes up and I click Save to Disk. It then “downloads” instantly and I can’t open the file on my PC. It’s obviously not downloading the file because it’s an 18 MB file and it takes < 1 second to “download.”
Here’s my script, with a few folder names changed for security:
<!--#include file="fsoHeader.asp"-->
<% 'Option Explicit %>
<% Response.Buffer=True %>
<%
Const FOLDER_PATH="..\audio_downloads\hidden10258\folder_2\even_more_secured1x"
'full path to the secure folder
Dim strFileName 'name of file
strFileName=Request.Querystring("file")
If Len(strFileName)>0 Then
Call DownloadFile (strFileName, Request.Querystring("force"))
End If
Sub DownloadFile(strFileName, blnForceDownload)
Dim fso, objFile, strFilePath
Dim fileSize, blnBinary, strContentType
Dim objStream, strAllFile
'----------------------
'first step: verify the file exists
'----------------------
'build file path:
strFilePath=FOLDER_PATH
' add backslash if needed:
If Right(strFilePath, 1)<>"\" Then strFilePath=strFilePath&"\"
strFilePath=strFilePath&strFileName
'initialize file system object:
Set fso=Server.CreateObject("Scripting.FileSystemObject")
'check that the file exists:
If Not(fso.FileExists(Map((strFilePath)))) Then
Set fso=Nothing
Err.Raise 20000, "File Downloader", "Error: file does not exist on the system: "&strFilePath
Response.END
End If
'----------------------
'second step: get file size.
'----------------------
Set objFile=fso.GetFile(Map(strFilePath))
fileSize=objFile.Size
Set objFile=Nothing
'----------------------
'third step: check whether file is binary or not and get content type of the file. (according to its extension)
'----------------------
blnBinary=GetContentType(strFileName, strContentType)
strAllFile=""
'----------------------
'fourth step: read the file contents.
'----------------------
If blnBinary Then
Set objStream=Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 1 'adTypeBinary
objStream.LoadFromFile (Map(strFilePath))
strAllFile=objStream.Read(fileSize)
objStream.Close
Set objStream = Nothing
Else
Set objFile=fso.OpenTextFile(Map(strFilePath),1) 'forReading
If Not(objFile.AtEndOfStream) Then
strAllFile=objFile.ReadAll
End If
objFile.Close
Set objFile=Nothing
End If
'----------------------
'final step: apply content type and send file contents to the browser
'----------------------
If blnForceDownload="true" Then
Response.AddHeader "Content-Disposition", "attachment; filename="&strFileName
End If
Response.AddHeader "Content-Length", fileSize
Response.Charset = "UTF-8"
Response.ContentType = strContentType
If blnBinary Then
Response.BinaryWrite(strAllFile)
Else
Response.Write(strAllFile)
End If
'clean up:
Set fso=Nothing
Response.Flush
Response.END
End Sub
Function GetContentType(ByVal strName, ByRef ContentType)
'return whether binary or not, put type into second parameter
Dim strExtension
strExtension="."&GetExtension(strName)
Select Case strExtension
Case ".mp3"
ContentType = "audio/mpeg3"
GetContentType=True
Case ".mpg", ".mpeg"
ContentType = "video/mpeg"
GetContentType=True
Case ".txt"
ContentType = "text/plain"
GetContentType=False
Case Else
'Handle All Other Files
ContentType = "application/octet-stream"
GetContentType=True
End Select
End Function
Function GetExtension(strName)
Dim arrTmp
arrTmp=Split(strName, ".")
GetExtension=arrTmp(UBound(arrTmp))
End Function
Response.Write("Connecting...")
%>
Here’s the code for the include file at the top of the page:
<%
function Map(f)
Map = Server.MapPath(f)
end function
%>
I am baffled as to why it’s not downloading this properly. Any help would be appreciated.