Connecting Tech Pros Worldwide Help | Site Map

download file using asp

colinod's Avatar
Familiar Sight
 
Join Date: Nov 2007
Posts: 182
#1: Feb 24 '09
I am trying to get my site to download mp3 files without having to right click - save as on a link.
I have found this code, which works as long as the file names are short, if they get a bit long it just opens a blank page, can anyone help

Expand|Select|Wrap|Line Numbers
  1. <%@Language="VBScript"%>
  2. <%Option Explicit%>
  3. <%Response.Buffer = True%>
  4. <%
  5. On Error Resume Next
  6. Dim strPath
  7. strPath = CStr(Request.QueryString("file"))
  8. '-- do some basic error checking for the QueryString
  9. If strPath = "" Then
  10.     Response.Clear
  11.     Response.Write("No file specified.")
  12.     Response.End
  13. ElseIf InStr(strPath, "..") > 0 Then
  14.     Response.Clear
  15.     Response.Write("Illegal folder location.")
  16.     Response.End
  17. ElseIf Len(strPath) > 1024 Then
  18.     Response.Clear
  19.     Response.Write("Folder path too long.")
  20.     Response.End
  21. Else
  22.     Call DownloadFile(strPath)
  23. End If
  24.  
  25. Private Sub DownloadFile(file)
  26.     '--declare variables
  27.     Dim strAbsFile
  28.     Dim strFileExtension
  29.     Dim objFSO
  30.     Dim objFile
  31.     Dim objStream
  32.     '-- set absolute file location
  33.     strAbsFile = Server.MapPath(file)
  34.     '-- create FSO object to check if file exists and get properties
  35.     Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
  36.     '-- check to see if the file exists
  37.     If objFSO.FileExists(strAbsFile) Then
  38.         Set objFile = objFSO.GetFile(strAbsFile)
  39.         '-- first clear the response, and then set the appropriate headers
  40.         Response.Clear
  41.         '-- the filename you give it will be the one that is shown
  42.         ' to the users by default when they save
  43.         Response.AddHeader "Content-Disposition", "attachment; filename=" & objFile.Name
  44.         Response.AddHeader "Content-Length", objFile.Size
  45.         Response.ContentType = "application/octet-stream"
  46.         Set objStream = Server.CreateObject("ADODB.Stream")
  47.         objStream.Open
  48.         '-- set as binary
  49.         objStream.Type = 1
  50.         Response.CharSet = "UTF-8"
  51.         '-- load into the stream the file
  52.         objStream.LoadFromFile(strAbsFile)
  53.         '-- send the stream in the response
  54.         Response.BinaryWrite(objStream.Read)
  55.         objStream.Close
  56.         Set objStream = Nothing
  57.         Set objFile = Nothing
  58.     Else 'objFSO.FileExists(strAbsFile)
  59.         Response.Clear
  60.         Response.Write("No such file exists.")
  61.     End If
  62.     Set objFSO = Nothing
  63. End Sub
  64. %>
The link is like this to the download.asp page

Expand|Select|Wrap|Line Numbers
  1. <a href="../download.asp?file=/mp3/<%=dlRecordset("filename")%>.mp3" class="downloadtext">
and when it does not work the url is like the following

Expand|Select|Wrap|Line Numbers
  1. http://www.yaketyyakallmouth.com/download.asp?file=/mp3/ALTMAN%20-%20%20BRIDE%20AND%20GROOMING%20(LIGHTHEARTED,%20TONGUE%20IN%20CHEEK).mp3
could it be the spaces??? the code replaces the spaces with an underscore when it does save

thanks for any ideas or help
colinod's Avatar
Familiar Sight
 
Join Date: Nov 2007
Posts: 182
#2: Feb 24 '09

re: download file using asp


I have found the problem, it does not work if there is a comma in the file name, does anyone know a way round this???? I have over 2000 files and lots have commas in the file name
jhardman's Avatar
Moderator
 
Join Date: Jan 2007
Location: logan, utah
Posts: 2,690
#3: Feb 26 '09

re: download file using asp


Quote:

Originally Posted by colinod View Post

I have found the problem, it does not work if there is a comma in the file name, does anyone know a way round this???? I have over 2000 files and lots have commas in the file name

Write a script to rename them. There should be no commas in file names regardless...

Jared
colinod's Avatar
Familiar Sight
 
Join Date: Nov 2007
Posts: 182
#4: Mar 2 '09

re: download file using asp


thats what i thought thanks
Reply


Similar ASP / Active Server Pages bytes