473,738 Members | 11,146 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

download file using asp

347 Contributor
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 6461
colinod
347 Contributor
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 Recognized Expert Specialist
@colinod
Write a script to rename them. There should be no commas in file names regardless...

Jared
Feb 26 '09 #3
colinod
347 Contributor
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
7206
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 using SAFileUp version 3.2. I ran through their documentation and found a section called "secure download", unfortunately, their examples do not work at all! On Netscape 7, the file "pretends" to download (after changing the file
6
8537
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 all works fine. If the user selects "Open" or "Cancel", the window closes, which is the desired behavior. If they select "Save", the location dialog shows up, they save the file and the window remains open. I've tried a number of things to get...
0
3041
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 cPanel URL that delivers a gzipped copy of the log file with all entires for the current month (up to the current date and time). I want to run this script once a day (at night) and would like to download the gzipped file by resuming from where...
0
1505
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 download process works OK, it adds a few bytes to the file during the download, so a 46k file becomes 52k. I can still open the file using Winzip, but cannot open it using my active X extraction system (ActiveXZip from www.hdse.com). I get an error...
1
2255
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 via ftp. Now I'm using the http procedures to have my application download zip files from my website. Although the download process works OK, it adds a few bytes to the file during the download, so a 46k file becomes 52k. I can still open the file...
2
1945
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 (which I have copied with some variable name changes) from a book on ASP.NET programming. This code sits in the Page_Load event of a file called 'Download.aspx'. In order to send a file to the client, this page is called from another page (using...
4
1848
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 the window because the response is sending the file and so no new requests can be made. what possibilities do i have to make the client able to browse other pages in my app while downloading a file? Thanks in advance.
4
2334
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 dialog boxes that says something like "Do you want to download FILEX.txt?" I want the user to be able to download the file rather than have the file displayed in the browser, because the file will include some characters such as tabs and commas....
3
9739
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 lot of information around the web, however when it comes to dowload files from a remote server (NOT hosted in my server) there is no information :-(, the idea is to have an Access DB that stores the full url...
1
47476
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 on a link and after a moment or two a file download dialog box pops-up in your web browser and prompts you for some instructions, such as “open” or “save“. I’m going to show you how to do that using a perl script. What You Need Any recent...
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9476
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.