473,320 Members | 2,041 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

download file using asp

347 100+
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
Feb 24 '09 #1
3 6251
colinod
347 100+
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
Feb 24 '09 #2
jhardman
3,406 Expert 2GB
@colinod
Write a script to rename them. There should be no commas in file names regardless...

Jared
Feb 26 '09 #3
colinod
347 100+
thats what i thought thanks
Mar 2 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Dave | last post by:
Hi Everyone, I was wondering if anyone has ever successfully used SA-FileUp's download feature. I am being forced to change from "ASPSmartUpload" as the server I am moving everything to is...
6
by: chon | last post by:
I have an ASP page that is sent a file location as a parameter. It opens this file, loads the ADODB.Stream object and does a binary write to the page forcing the download dialog to appear. This...
0
by: Chuck Anderson | last post by:
I am writing a Php script to run on my home PC (Windows) that downloads an Apache access log file and inserts new entries into a database.. The only way I can access these log files is through a...
0
by: Owen Jenkins | last post by:
I've been using Dev's fantastic InetTransferLib to upload and download files via ftp. Now I'm using the http procedures to have my application download zip files from my website. Although the...
1
by: Owen Jenkins | last post by:
Reposting this message from last week since I have not had a reply so far. Hopefully someone will have an idea? --- I've been using Dev's fantastic InetTransferLib to upload and download files...
2
by: Sleepy | last post by:
I am wanting to allow a client to download a file from a web server and save it on their local PC. The only way that I have found to do this is to use HTTP headers. I am using the code shown below...
4
by: hoenes1 | last post by:
Hi, in my ASP.NET App, I have a list of files which the user can download on click (using Response.BinaryWrite()). the problem is, that once a download has started, the user can't navigate in...
4
by: Nathan Sokalski | last post by:
I want to give visitors to my site the option of downloading a generated ..txt file by clicking a button. I know how to generate text files, but how do I cause the browser to pop up one of those...
3
by: axelman | last post by:
Hi guys, I'm using Classic ASP, IIS6, IE 7, FF 3 I've developed a vb script to downloand files hosted in my server (zip, doc, pdf, jpg, xls, et. etc.) it's very straight forward and there's a ...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.