#16
  1. No Profile Picture
    Contributing User
    ASP Mastermind (5000+ posts)

    Join Date
    Aug 2005
    Location
    North East, UK
    Posts
    6,191
    Rep Power
    140
    I've just tested with the same results.

    Example
    http://computer-helpforum.com/asp/Fo...adExample.html
    (idserve.exe Download file (forcing Save As box))

    file = http://computer-helpforum.com/asp/de...le=idserve.exe
  2. #17
  3. No Profile Picture
    Contributing User
    ASP Mastermind (5000+ posts)

    Join Date
    Aug 2005
    Location
    North East, UK
    Posts
    6,191
    Rep Power
    140
    Here is a small addition to the script just to show the difference between the three methods of opening the file

    false - will try to stream the file and output the contents to the browser
    true - will stream the file and prompt the user
    redirect - will simply open the the redirect to the file URL in the browser. The Browser/PC will then decide what to do.
    you will see the full effect of this method if you test the doc or pdf and you have office or reader installed and configured to integrate into the browser.
    Code:
    <p><a href="DownloadManager.asp?file=file.txt&force=false" target="_blank">txt Download file without forcing Save As box (false) </a><br />
      <a href="DownloadManager.asp?file=file.txt&force=true" target="_blank">txt Download file forcing Save As box (true) </a> <br>
      <a href="DownloadManager.asp?file=file.txt&force=redirect" target="_blank">txt Download file (redirect)</a> </p>
    Code:
    http://domain.com/ShadowDownload/DownloadManager.asp?file=file.txt&force=false
    http://domain.com/ShadowDownload/DownloadManager.asp?file=file.txt&force=true
    http://domain.com/ShadowDownload/DownloadManager.asp?file=file.txt&force=redirect
    asp Code:
    'Constants
    Const URL = "http://domain.com/ShadowDownload/"
    Const FOLDER_PATH="e:\domains\d\domain.com\user\htdocs\S  hadowDownload" 'full path to the secure folder
    Const adTypeBinary = 1
    Const adTypeText = 2
    Const chunkSize = 2048
    Const LOG_FILE_NAME="DownloadManagerLog.txt"
    Const LOG_ACTIVE=True
     
    Dim strFileName 'name of the file to be downloaded
    Dim blnForceDownload
    Dim iSz, i 'used to write in chunks
    strFileName=Request("file")
    blnForceDownload = Request("force")
     
    If Len(strFileName)>0 Then
    	If blnForceDownload = "redirect" Then
    		Response.Redirect(URL & strFileName)
    	Else
    		Call DownloadFile (strFileName, blnForceDownload)
    	End If
    End If


    Here is an example
    http://computer-helpforum.com/asp/Fo...adExample.html
  4. #18
  5. No Profile Picture
    Contributing User
    ASP Discoverer (100 - 499 posts)

    Join Date
    Nov 2003
    Posts
    111
    Rep Power
    15
    hi anyway to use this to download more then one file at a time.
    (I want to let the user choose which files to download from a list of filenames and then download just those files
  6. #19
  7. Moderator From Beyond
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,135
    Rep Power
    2925
    Originally Posted by esthera
    hi anyway to use this to download more then one file at a time.
    (I want to let the user choose which files to download from a list of filenames and then download just those files
    no, this is not possible both technically and logically.
    the only way is zipping the files on the server, let
    user download the zip file and tell him to unzip it.
  8. #20
  9. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Mar 2007
    Posts
    3
    Rep Power
    0

    file does not exist


    Hi, I have been trying to get this asp file to work, but am having no luck. I am trying to force the download of a jpeg. Whenever I call it it states "file does not exist". However when i copy and paste the link which it says does not exist the file comes up fine in the browser.

    Is there something obvious i am missing?

    Thanks for any help
  10. #21
  11. Moderator From Beyond
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,135
    Rep Power
    2925
    post your code and we'll see.
  12. #22
  13. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Mar 2007
    Posts
    3
    Rep Power
    0

    My code


    Here is the code i'm using:


    [CODE]--DownloadManager.asp
    <% Option Explicit %>
    <% Response.Buffer=True %>
    <%
    'Constants
    Code:
    Const FOLDER_PATH="http://myserver/folder1/folder2"
    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
    %>

    and to call it:


    <a href="DownloadManager.asp?file=bottom.jpg&force=tr ue">Download file (forcing Save As box)</a>



    Thankyou for any help
  14. #23
  15. Moderator From Beyond
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,135
    Rep Power
    2925
    the FOLDER_PATH is wrong. it must be valid path on the server disk, not URL.
    this code is not meant for taking files from different servers, for this
    you need whole different code, look for XMLHTTP components.
  16. #24
  17. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Mar 2007
    Posts
    3
    Rep Power
    0
    I see. Is it possible to work out this location myself, or is it generally something I would have to find out from the company which hosts the site?

    Thanks again
  18. #25
  19. Moderator From Beyond
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,135
    Rep Power
    2925
    use MapPath, for example:
    Code:
    Server.MapPath("folder1/folder2")
    will give the full path on the disk of that folder.

    Comments on this post

    • AdrianM85 agrees
  20. #26
  21. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    May 2007
    Posts
    4
    Rep Power
    0

    Thumbs up Downloaded File Name is Always DownloadManager.asp


    Gr8 Script. Thnx. v much 4 sharing it.


    And n order 2 enhance it even more allow me 2 share with u my experience:
    1. The downloaded Filename is always the name of the script fetching it
    2. The component
      Code:
      Response.Buffer = False
      in line 77 didn't work in 2 different servers.


    Other than that. It is a gr8 tool that i thank u 4 sharing with all of us.
  22. #27
  23. Moderator From Beyond
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,135
    Rep Power
    2925
    I have now tested this for myself and it works just fine, saving
    the file in the file original name. please provide more details and
    post your actual code, maybe you changed something.

    regarding the Response.Buffer go to IIS configuration console,
    right click your website --> Home Directory --> Configuration -->
    Options --> tick the Enable Buffering checkbox and confirm.

    anyway, glad you found my code useful.
  24. #28
  25. No Profile Picture
    Contributing User
    ASP Mastermind (5000+ posts)

    Join Date
    Aug 2005
    Location
    North East, UK
    Posts
    6,191
    Rep Power
    140
    The downloaded Filename is always the name of the script fetching it
    Check that the path is correct. If you are missing a trailing backslash then it can corrupt the script.
  26. #29
  27. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    May 2007
    Posts
    4
    Rep Power
    0

    Smile


    Thank you guys 4 ur responses. The file name is no prob in the remote server hosting my website. Maybe it was the \\servername path that i used on my local server rather than the absolute path that triggered it! anyway...

    It is worth mentioning that when i contacted my remote host they told me that /secure should work without having 2 specify any pre-path. I tried it & it didn't work with this script

    Another problem that rouse now is the file size downloaded is always 0 byte. I'm guessing it is a permission issue on the file itself or the folder & already contacted my host support.

    Thnx again
  28. #30
  29. Moderator From Beyond
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,135
    Rep Power
    2925
    cheers buddy, if there are any problems related to the code don't
    hesitate to report.

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

IMN logo majestic logo threadwatch logo seochat tools logo