Notices

Great community. Great ideas.

Welcome to ASP/Free, a community dedicated to helping beginners and professionals alike in improving their knowledge of Microsoft's development and administration technologies. Sign up today to gain access to the combined insight of tens of thousands of members.

Thread: Download Manager: downloading files from secure location (classic ASP)

Page 1 of 6 123 ... LastLast
Results 1 to 15 of 90
Share This Thread →
  1. #1
    Shadow Wizard's Avatar
    Shadow Wizard is offline Moderator From Beyond
    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,126
    Rep Power
    2921

    Post Download Manager: downloading files from secure location (classic ASP)

    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/general...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.
    Attached Files Attached Files
    Last edited by Shadow Wizard; February 5th, 2012 at 02:54 AM. Reason: Fixed bug not allowing Firefox to force download file with space in its name

  2. #2
    esthera is offline Contributing User
    Join Date
    Nov 2003
    Posts
    111
    Rep Power
    11
    I tried teh code located at http://forums.aspfree.com/code-bank-...=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. #3
    Shadow Wizard's Avatar
    Shadow Wizard is offline Moderator From Beyond
    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,126
    Rep Power
    2921
    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. #4
    esthera is offline Contributing User
    Join Date
    Nov 2003
    Posts
    111
    Rep Power
    11
    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. #5
    degsy is offline Contributing User
    Join Date
    Aug 2005
    Location
    North East, UK
    Posts
    6,191
    Rep Power
    136
    Have you tried
    Code:
    DownloadFile ("test.pdf","true")
    The script is checking for a string not a boolean.

  6. #6
    esthera is offline Contributing User
    Join Date
    Nov 2003
    Posts
    111
    Rep Power
    11
    I tried and that didn't work -- it still outputs garbage to the screen with no save as

    Quote Originally Posted by degsy
    Have you tried
    Code:
    DownloadFile ("test.pdf","true")
    The script is checking for a string not a boolean.

  7. #7
    Shadow Wizard's Avatar
    Shadow Wizard is offline Moderator From Beyond
    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,126
    Rep Power
    2921
    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. #8
    esthera is offline Contributing User
    Join Date
    Nov 2003
    Posts
    111
    Rep Power
    11
    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. #9
    Shadow Wizard's Avatar
    Shadow Wizard is offline Moderator From Beyond
    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,126
    Rep Power
    2921
    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. #10
    Shadow Wizard's Avatar
    Shadow Wizard is offline Moderator From Beyond
    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,126
    Rep Power
    2921
    code has been improved, now it can force the download of big files.
    thanks mehere!

  11. #11
    red_fiesta is offline Contributing User
    Join Date
    Oct 2006
    Posts
    131
    Rep Power
    8
    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

  12. #12
    Shadow Wizard's Avatar
    Shadow Wizard is offline Moderator From Beyond
    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,126
    Rep Power
    2921
    thanks for the heads up, the code has been fixed now.. please
    take the new version and it should work fine.
    (I have tested it under IE and Firefox)

  13. #13
    mymanley is offline Registered User
    Join Date
    Jan 2007
    Posts
    2
    Rep Power
    0

    Track number of downloads

    I hope this is not a stupid question, but how do you track the number times that the file has been downloaded?

  14. #14
    mymanley is offline Registered User
    Join Date
    Jan 2007
    Posts
    2
    Rep Power
    0

    Exe problems

    Also, I'm trying to use this code for an executable (.exe) file, the Content-type application/octet-stream should work. However, I'm getting an error when I try to run the .exe after using DownLoadManager.asp to get the file.

    I've verified that the exe is fine - or at least it works if I copy it from the Web server and place it directly on my desktop.

    Do you have any thoughts as to why that might be?

  15. #15
    Shadow Wizard's Avatar
    Shadow Wizard is offline Moderator From Beyond
    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,126
    Rep Power
    2921
    Quote Originally Posted by mymanley
    I hope this is not a stupid question, but how do you track the number times that the file has been downloaded?
    this is not something simple.
    if you want permanent log of the downloads you'll have to use
    database or text files. are you familiar with any of those?

    lso, I'm trying to use this code for an executable (.exe) file, the Content-type application/octet-stream should work. However, I'm getting an error when I try to run the .exe after using DownLoadManager.asp to get the file.

    I've verified that the exe is fine - or at least it works if I copy it from the Web server and place it directly on my desktop.

    Do you have any thoughts as to why that might be?
    did you try with different exe file? does it happen with other files?
    try placing direct link to the file:
    Code:
    <a href="something.exe">download</a>
    and try to download - can you activate the file after saving it?

Page 1 of 6 123 ... LastLast
Share This Thread →

Become Part of This Conversation

Join NowFor Free!

Similar Threads

  1. File Download problems
    By clboren in forum ASP Development
    Replies: 0
    Last Post: March 15th, 2005, 04:54 PM
  2. Issue downloading large files
    By Ceuphrasie in forum ASP Development
    Replies: 13
    Last Post: March 14th, 2005, 10:33 AM
  3. Download Two Files From One Button?
    By Currie in forum .NET Development
    Replies: 1
    Last Post: February 5th, 2005, 03:56 AM
  4. Downloading files
    By Zil in forum .NET Development
    Replies: 7
    Last Post: January 8th, 2005, 08:23 AM
  5. FTP download a number of files with same extension
    By otarboy in forum Visual Basic Programming
    Replies: 1
    Last Post: September 29th, 2004, 11:27 AM

ASP Free Advertisers and Affiliates