download file using asp

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • colinod
    Contributor
    • Nov 2007
    • 347

    download file using asp

    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

    Code:
    <%@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
    %>
    The link is like this to the download.asp page

    Code:
    <a href="../download.asp?file=/mp3/<%=dlRecordset("filename")%>.mp3" class="downloadtext">
    and when it does not work the url is like the following

    Code:
    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
    Contributor
    • Nov 2007
    • 347

    #2
    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

    Comment

    • jhardman
      Recognized Expert Specialist
      • Jan 2007
      • 3405

      #3
      Originally posted by colinod
      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

      Comment

      • colinod
        Contributor
        • Nov 2007
        • 347

        #4
        thats what i thought thanks

        Comment

        Working...