| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread |
Rating:
|
Display Modes |
|
#1
|
||||
|
||||
|
use this code to download files from secure folder on the server i.e. folder which is not accessible from the website.
this allow you to: 1. protect files by adding user validation to the page. 2. count how many times each file has been downloaded. the code is provided as-is only change the constant of the secure folder path. two download methods are available: "normal" download which send the file to the browser as-is (e.g. if it's htm/text it would open in the browser itself) or "force download" mode in which the Save As dialog box appear and user can save the file. Code:
--DownloadManager.asp
<% Option Explicit %>
<% Response.Buffer=True %>
<%
'Constants
Const FOLDER_PATH="c:\Secure" 'full path to the secure folder
Const adTypeBinary = 1
Const adTypeText = 2
Const chunkSize = 2048
'global variables:
Dim strFileName 'name of the file to be downloaded
Dim blnForceDownload 'force the download or allow the file to be opened in the browser?
'read file from querystring:
strFileName=Request("file")
'force download?
blnForceDownload = (Request("force")="true")
'execute the function if we got anything:
If Len(strFileName)>0 Then
Call DownloadFile (strFileName, blnForceDownload)
Response.END
End If
Sub DownloadFile(strFileName, blnForceDownload)
'local variables:
Dim fso, objFile, strFilePath
Dim fileSize, blnBinary, strContentType
Dim objStream, strAllFile, iSz
Dim i
'----------------------
'first step: verify the file exists
'----------------------
'build file path:
strFilePath=FOLDER_PATH
' add backslash if needed:
If Right(strFilePath, 1)<>"\" Then strFilePath=strFilePath&"\"
strFilePath=strFilePath&strFileName
'initialize file system object:
Set fso=Server.CreateObject("Scripting.FileSystemObject")
'check that the file exists:
If Not(fso.FileExists(strFilePath)) Then
Set fso=Nothing
Err.Raise 20000, "Download Manager", "Fatal Error: file does not exist: "&strFilePath
Response.END
End If
'----------------------
'second step: get file size.
'----------------------
Set objFile=fso.GetFile(strFilePath)
fileSize=objFile.Size
Set objFile=Nothing
'----------------------
'third step: check whether file is binary or not and get content type of the file. (according to its extension)
'----------------------
blnBinary=GetContentType(strFileName, strContentType)
strAllFile=""
'----------------------
'forth step: read the file contents.
'----------------------
'force download? if so, add proper header:
If blnForceDownload Then
Response.AddHeader "Content-Disposition", "attachment; filename="&strFileName
End If
If blnBinary Then
Set objStream=Server.CreateObject("ADODB.Stream")
'Added to breakup chunk
Response.Buffer = False
'this might be long...
Server.ScriptTimeout = 30000
'----------------------
objStream.Open
objStream.Type = 1 'adTypeBinary
objStream.LoadFromFile strFilePath
'Added to breakup chunk
iSz = objStream.Size
Response.AddHeader "Content-Length", iSz
Response.Charset = "UTF-8"
Response.ContentType = strContentType
For i = 1 To iSz \ chunkSize
If Not Response.IsClientConnected Then Exit For
Response.BinaryWrite objStream.Read(chunkSize)
Next
If iSz Mod chunkSize > 0 Then
If Response.IsClientConnected Then
Response.BinaryWrite objStream.Read(iSz Mod chunkSize)
End If
End If
objStream.Close
Set objStream = Nothing
'--------------------------------------
'Commented out Original Code
'strAllFile=objStream.Read(fileSize)
'objStream.Close
'Set objStream = Nothing
'--------------------------------------
Else
Set objFile=fso.OpenTextFile(strFilePath,1) 'forReading
If Not(objFile.AtEndOfStream) Then
strAllFile=objFile.ReadAll
End If
objFile.Close
Set objFile=Nothing
Response.Write(strAllFile)
End If
'clean up:
Set fso=Nothing
Response.Flush
Response.END
End Sub
Function GetContentType(ByVal strName, ByRef ContentType)
'return whether binary or not, put type into second parameter
Dim strExtension
strExtension="."&GetExtension(strName)
Select Case strExtension
Case ".asf"
ContentType = "video/x-ms-asf"
GetContentType=True
Case ".avi"
ContentType = "video/avi"
GetContentType=True
Case ".doc"
ContentType = "application/msword"
GetContentType=True
Case ".zip"
ContentType = "application/zip"
GetContentType=True
Case ".xls"
ContentType = "application/vnd.ms-excel"
GetContentType=True
Case ".gif"
ContentType = "image/gif"
GetContentType=True
Case ".jpg", ".jpeg"
ContentType = "image/jpeg"
GetContentType=True
Case ".wav"
ContentType = "audio/wav"
GetContentType=True
Case ".mp3"
ContentType = "audio/mpeg3"
GetContentType=True
Case ".wma"
ContentType = "audio/wma"
GetContentType=True
Case ".mpg", ".mpeg"
ContentType = "video/mpeg"
GetContentType=True
Case ".pdf"
ContentType = "application/pdf"
GetContentType=True
Case ".rtf"
ContentType = "application/rtf"
GetContentType=True
Case ".htm", ".html"
ContentType = "text/html"
GetContentType=False
Case ".asp"
ContentType = "text/asp"
GetContentType=False
Case ".txt"
ContentType = "text/plain"
GetContentType=False
Case Else
'Handle All Other Files
ContentType = "application/octet-stream"
GetContentType=True
End Select
End Function
Function GetExtension(strName)
Dim arrTmp
arrTmp=Split(strName, ".")
GetExtension=arrTmp(UBound(arrTmp))
End Function
%>
Code:
--DownloadExample.asp <html> <head> <title>Download File Example</title> </head> <body> <a href="DownloadManager.asp?file=testfile.txt&force=false">Download file without forcing Save As box</a><br /> <a href="DownloadManager.asp?file=testfile.txt&force=true">Download file (forcing Save As box)</a> </body> </html> edit: (23/10/2006) Thanks to mehere, I have improved the code so that it can force the download of big files. credit is given to this article: http://classicasp.aspfaq.com/genera...-mime-type.html edit: (13/11/2006) Thanks to red_fiesta, I have fixed the code so that files can also be opened by the browser, without forcing download. Last edited by Shadow Wizard : November 13th, 2006 at 05:42 AM. |
|
#2
|
|||
|
|||
|
I tried teh code located at http://forums.aspfree.com/code-bank...t=download+file
it works great for showing the file but when I try it with forcedownload=true it just shows garbage on the browser and I don't get any save to disk option. why? Last edited by Shadow Wizard : July 4th, 2006 at 07:55 AM. |
|
#3
|
||||
|
||||
|
I double checked it now and it works fine.
post your code and I'll try to help you find and solve the problem. |
|
#4
|
|||
|
|||
|
my code is almost identical to yours - i just call the function not using the request object.
it works if i call dowloadfile with false but if I set it to true I get garbage on the webbrowser and no save to disk prompt. Code:
<% Option Explicit %>
<% Response.Buffer=True %>
<%
Call DownloadFile ("test.pdf",true)
Const FOLDER_PATH="D:\esther\files\" 'full path to the secure folder
Dim strFileName 'name of the file to be downloaded
strFileName=Request("file")
Sub DownloadFile(strFileName, blnForceDownload)
Dim fso, objFile, strFilePath
Dim fileSize, blnBinary, strContentType
Dim objStream, strAllFile
'----------------------
'first step: verify the file exists
'----------------------
'build file path:
strFilePath=FOLDER_PATH
' add backslash if needed:
If Right(strFilePath, 1)<>"\" Then strFilePath=strFilePath&"\"
strFilePath=strFilePath&strFileName
'initialize file system object:
Set fso=Server.CreateObject("Scripting.FileSystemObject")
'check that the file exists:
If Not(fso.FileExists(strFilePath)) Then
Set fso=Nothing
Err.Raise 20000, "Download Manager", "Fatal Error: file does not exist: "&strFilePath
Response.END
End If
'----------------------
'second step: get file size.
'----------------------
Set objFile=fso.GetFile(strFilePath)
fileSize=objFile.Size
Set objFile=Nothing
'----------------------
'third step: check whether file is binary or not and get content type of the file. (according to its extension)
'----------------------
blnBinary=GetContentType(strFileName, strContentType)
strAllFile=""
'----------------------
'forth step: read the file contents.
'----------------------
If blnBinary Then
Set objStream=Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 1 'adTypeBinary
objStream.LoadFromFile strFilePath
strAllFile=objStream.Read(fileSize)
objStream.Close
Set objStream = Nothing
Else
Set objFile=fso.OpenTextFile(strFilePath,1) 'forReading
If Not(objFile.AtEndOfStream) Then
strAllFile=objFile.ReadAll
End If
objFile.Close
Set objFile=Nothing
End If
'----------------------
'final step: apply content type and send file contents to the browser
'----------------------
If blnForceDownload="true" Then
Response.AddHeader "Content-Disposition", "attachment; filename="&strFileName
End If
Response.AddHeader "Content-Length", fileSize
Response.Charset = "UTF-8"
Response.ContentType = strContentType
If blnBinary Then
Response.BinaryWrite(strAllFile)
Else
Response.Write(strAllFile)
End If
'clean up:
Set fso=Nothing
Response.Flush
Response.END
End Sub
Function GetContentType(ByVal strName, ByRef ContentType)
'return whether binary or not, put type into second parameter
Dim strExtension
strExtension="."&GetExtension(strName)
Select Case strExtension
Case ".asf"
ContentType = "video/x-ms-asf"
GetContentType=True
Case ".avi"
ContentType = "video/avi"
GetContentType=True
Case ".doc"
ContentType = "application/msword"
GetContentType=True
Case ".zip"
ContentType = "application/zip"
GetContentType=True
Case ".xls"
ContentType = "application/vnd.ms-excel"
GetContentType=True
Case ".gif"
ContentType = "image/gif"
GetContentType=True
Case ".jpg", ".jpeg"
ContentType = "image/jpeg"
GetContentType=True
Case ".wav"
ContentType = "audio/wav"
GetContentType=True
Case ".mp3"
ContentType = "audio/mpeg3"
GetContentType=True
Case ".mpg", ".mpeg"
ContentType = "video/mpeg"
GetContentType=True
Case ".rtf"
ContentType = "application/rtf"
GetContentType=True
Case ".htm", ".html"
ContentType = "text/html"
GetContentType=False
Case ".asp"
ContentType = "text/asp"
GetContentType=False
Case ".txt"
ContentType = "text/plain"
GetContentType=False
Case Else
'Handle All Other Files
ContentType = "application/octet-stream"
GetContentType=True
End Select
End Function
Function GetExtension(strName)
Dim arrTmp
arrTmp=Split(strName, ".")
GetExtension=arrTmp(UBound(arrTmp))
End Function
%>
|
|
#5
|
|||
|
|||
|
Have you tried
Code:
DownloadFile ("test.pdf","true")
The script is checking for a string not a boolean.
__________________
CyberTechHelp |
|
#6
|
|||
|
|||
|
I tried and that didn't work -- it still outputs garbage to the screen with no save as
Quote:
|
|
#7
|
||||
|
||||
|
works fine for me with PDF files as well so can't see what is the problem.
try adding those lines to GetContentType function in DownloadManager.asp file: Code:
Case ".pdf" ContentType = "application/pdf" GetContentType=True |
|
#8
|
|||
|
|||
|
thanks that did it..
now we also want to enable downloadinof a .max file (paperport) but currently it's not working - it asked me to select what program to open the file with. I selected Paperport, but it still didn't work. anyideas what I can do? |
|
#9
|
||||
|
||||
|
no idea what .max extension is, probably what needed to run it is not
installed on your computer either. try Saving the file to your disk then Open it. |
|
#10
|
||||
|
||||
|
code has been improved, now it can force the download of big files.
thanks mehere! ![]() |
|
#11
|
|||
|
|||
|
i used the code that was provided (at the top, the example) and
whether is use Download file without forcing Save As box or Download file (forcing Save As box) its brings up the save window... please advise thanks |