Pages: [1]
Print
Author Topic: Download any file...  (Read 3725 times)
Phillipe Calmet Williams
Jr. Member
**
Posts: 50



View Profile WWW
« on: May 04, 2009, 02:37:03 PM »

If you include a file in your post, it is because you want people to download it.

But when you include an image file, or text file, or even a pdf file, the browser will just show the file, instead of download it.  Undecided

THIS CODE will change that. When you click on the link, the browser will show you the normal "download file" dialog.

Obviousle, a little change in the code is needed, but it is very useful. Tongue

Regards.
Logged

Matt
BP Blog Developer/Musician
Administrator
Newbie
*****
Posts: 36



View Profile WWW Email
« Reply #1 on: May 04, 2009, 06:14:57 PM »

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
%>

Quote
Copy the code above and save it as download.asp on your web server.

To use this code, you link to the page, passing the location of the file you want to send to the user. For example:
Code:
<a href="download.asp?file=/images/xefteri.gif">

The first part of the code does some basic error checking. The sub DownloadFile checks to see if the file is there, and if it is then it sends it as a binary stream using the ADODB Stream object. The header Content-Length is set so that the browser can properly display the progress bar.
Logged

Pages: [1]
Print
Jump to: