Page 6 of 6 First ... 456
  • Jump to page:
    #76
  1. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Jan 2012
    Posts
    7
    Rep Power
    0
    If i click OPEN it opens the test text file.
    If i remove the line of code i just get a blank page!
    This is driving me nuts! lol

    Originally Posted by Shadow Wizard
    No, this is browser behavior to avoid having invalid file name.


    Click "open" you might see some useful error message - in such case, post it here.

    Anyway, try to remove this line from the code:
    Code:
    Response.AddHeader "Content-Disposition", "attachment; filename="&strFileName
    Any change?
  2. #77
  3. Moderator From Beyond
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,135
    Rep Power
    2926
    Originally Posted by martin182003
    If i click OPEN it opens the test text file.
    If i remove the line of code i just get a blank page!
    This is driving me nuts! lol
    Sorry man... maybe it's like trying to use XP features on Windows 98 - the architecture is just too new for the poor old code.

    If you like (and have enough basic skill) I can throw quick ASP.NET code example that is doing exactly the same thing - let me know and I'll post it here soon in such case.
  4. #78
  5. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Jan 2012
    Posts
    7
    Rep Power
    0
    Anything is worth a go!
    Seems strange, because I had no problems changing from server 2003 to 2008, its just not working from 2008 to 2008 r2 yet they are both IIS version 7

    If you wouldnt mind getting some come up, i'll give it a go
    Much appreciated.
    Thanks.

    Originally Posted by Shadow Wizard
    Sorry man... maybe it's like trying to use XP features on Windows 98 - the architecture is just too new for the poor old code.

    If you like (and have enough basic skill) I can throw quick ASP.NET code example that is doing exactly the same thing - let me know and I'll post it here soon in such case.
  6. #79
  7. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Jan 2012
    Posts
    7
    Rep Power
    0
    Hi
    I think i have managed to get it working by playing about with the buffering within the file.
    However, now when I go to save the file it prefixes the file with an underscore before the file name i.e _test.pdf
    Can this be removed somehow?! as on my current server it doesnt have the underscore.
    in the following section of code I changed Response.Buffer = False to True and it works :S

    If blnBinary Then
    Set objStream=Server.CreateObject("ADODB.Stream")

    'Added to breakup chunk
    Response.Buffer = False

    Thanks.

    Originally Posted by martin182003
    Anything is worth a go!
    Seems strange, because I had no problems changing from server 2003 to 2008, its just not working from 2008 to 2008 r2 yet they are both IIS version 7

    If you wouldnt mind getting some come up, i'll give it a go
    Much appreciated.
    Thanks.

    Comments on this post

    • Shadow Wizard agrees : Many thanks :)
  8. #80
  9. Moderator From Beyond
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,135
    Rep Power
    2926
    Originally Posted by martin182003
    Hi
    I think i have managed to get it working by playing about with the buffering within the file.
    However, now when I go to save the file it prefixes the file with an underscore before the file name i.e _test.pdf
    Can this be removed somehow?! as on my current server it doesnt have the underscore.
    in the following section of code I changed Response.Buffer = False to True and it works :S

    If blnBinary Then
    Set objStream=Server.CreateObject("ADODB.Stream")

    'Added to breakup chunk
    Response.Buffer = False

    Thanks.
    Cool! Thanks for sharing.

    As for underscore it's because you got the file in sub folder of the "root" of the secure location. I didn't take this into account when writing the code.. to take only the file name first add this function to your code:

    Code:
    Function ExtractFileName(filePath)
    	Dim slashIndex
    	slashIndex = InStrRev(filePath, "\")
    	If slashIndex<1 Then
    		slashIndex = InStrRev(filePath, "/")
    	End If
    	If slashIndex>0 Then
    		ExtractFileName = Mid(filePath, slashIndex + 1, Len(filePath) - slashIndex + 1)
    	Else  
    		ExtractFileName = filePath
    	End If
    End Function
    Then change this line in my code:

    Code:
    strFilePath=strFilePath&strFileName
    To this instead:

    Code:
    strFilePath = strFilePath & ExtractFileName(strFileName)
    Happy Programming!
  10. #81
  11. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0
    I have this working to an extent... Thanks for the code!

    My issue (I didn't see it covered in the above posts) is that I'm passing file names which may have spaces in them IE (text file1.txt). I can't seem to get the download manager to do a "%20" or pass a space. am I missing something?
  12. #82
  13. Moderator From Beyond
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,135
    Rep Power
    2926
    Originally Posted by cteasen
    I have this working to an extent... Thanks for the code!

    My issue (I didn't see it covered in the above posts) is that I'm passing file names which may have spaces in them IE (text file1.txt). I can't seem to get the download manager to do a "%20" or pass a space. am I missing something?
    This should not matter. Just have the space in the link:
    Code:
    <a href="DownloadManager.asp?file=text file1.txt&force=false">Download file</a>
    The browser might encode it to "?file=text%20file1.txt" but it doesn't matter at all, since the ASP code will decode it back when reading Request("file") value.
    If you do have problems or error post the code you're using and full error message.
  14. #83
  15. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0
    Originally Posted by Shadow Wizard
    This should not matter. Just have the space in the link:
    Code:
    <a href="DownloadManager.asp?file=text file1.txt&force=false">Download file</a>
    The browser might encode it to "?file=text%20file1.txt" but it doesn't matter at all, since the ASP code will decode it back when reading Request("file") value.
    If you do have problems or error post the code you're using and full error message.

    This is the error I can't seem to get around.

    Download Manager error '800a4e20'

    Fatal Error: file does not exist: C:\dirlisting\New

    /DownloadMgr.asp, line 50
    This is my link I'm using, I have tried with just the file name and also by using a variable both with the same result.

    Code:
    Response.Write "<a href=DownloadMgr.asp?file=New Text Document.txt&force=true class=""docslink""><br>" ' Link to the file
    Response.write objFile.Name ' the file name
    Everything else is the same -here is the my path:

    Code:
    'Constants Const FOLDER_PATH="C:\dirlisting\" 'full path to the secure folder
     Const adTypeBinary = 1
     Const adTypeText = 2
     Const chunkSize = 2048
  16. #84
  17. Moderator From Beyond
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,135
    Rep Power
    2926
    You just need to add the double quotes properly:
    Code:
    Response.Write "<a href=""DownloadMgr.asp?file=New Text Document.txt&force=true"" class=""docslink""><br>"
    Happy programming!
  18. #85
  19. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0
    Originally Posted by Shadow Wizard
    You just need to add the double quotes properly:
    Code:
    Response.Write "<a href=""DownloadMgr.asp?file=New Text Document.txt&force=true"" class=""docslink""><br>"
    Happy programming!
    Oh man... I always do that. I always mess up some mundane detail.

    Thanks!
  20. #86
  21. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0
    Thanks for all your help on this so far Shadow Wizard... I have made a few changes to allow a user to download a file by listing what's in the directory and passing the link with added query string variables. Everything seems to work however it's now trying to save only the first word of a file. IE - "New Text.txt" when asked to save as it's only saving "New" with no extension.

    These spaces are going to be the death of me.

    Here is my code:
    Code:
    <% 'Option Explicit %>
    <% Response.Buffer=True %>
    <%
    
    'Path to use 
    FOLDER_PATH=("C:\" + Request.QueryString("P"))  'full path to the secure folder
    
    'Constants
    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")
    	
    	'Response.write Request ("P")
    	'Response.write strFileName
    
    '-----------------------------------
    'to get multiple query values	
    'for i=1 to Request.QueryString("P").Count
    '  Response.Write(Request.QueryString("P")(i) & "<br />")
    'next
    '----------------------------------
    
    '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 ".xls"
    			ContentType = "application/vnd.ms-excel"
    			GetContentType=True
    		Case ".gif"
    			ContentType = "image/gif"
    			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 here is my new link I'm passing-
    Code:
    strLe = Right(objFolder, Len(objFolder) -3)
    Response.Write "<a class=""docslink"" href=""DownloadMgr.asp?P=" & strLe & "\&file=" & objFile.Name & "&force=true"">
    Any help would be greatly appreciated
  22. #87
  23. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0
    Originally Posted by cteasen
    Thanks for all your help on this so far Shadow Wizard... I have made a few changes to allow a user to download a file by listing what's in the directory and passing the link with added query string variables. Everything seems to work however it's now trying to save only the first word of a file. IE - "New Text.txt" when asked to save as it's only saving "New" with no extension.

    These spaces are going to be the death of me.

    Here is my code:
    Code:
    <% 'Option Explicit %>
    <% Response.Buffer=True %>
    <%
    
    'Path to use 
    FOLDER_PATH=("C:\" + Request.QueryString("P"))  'full path to the secure folder
    
    'Constants
    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")
    	
    	'Response.write Request ("P")
    	'Response.write strFileName
    
    '-----------------------------------
    'to get multiple query values	
    'for i=1 to Request.QueryString("P").Count
    '  Response.Write(Request.QueryString("P")(i) & "<br />")
    'next
    '----------------------------------
    
    '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 ".xls"
    			ContentType = "application/vnd.ms-excel"
    			GetContentType=True
    		Case ".gif"
    			ContentType = "image/gif"
    			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 here is my new link I'm passing-
    Code:
    strLe = Right(objFolder, Len(objFolder) -3)
    Response.Write "<a class=""docslink"" href=""DownloadMgr.asp?P=" & strLe & "\&file=" & objFile.Name & "&force=true"">
    Any help would be greatly appreciated
    So here is an update...
    I was originally testing in firefox and found out that IE handles the links a little differently and will allow the saving of the full file name. Now I guess it's down to browser testing
    Any insight on why IE can save but Firefox would only try to save the first word in a file name?
  24. #88
  25. Moderator From Beyond
    ASP Mastermind (5000+ posts)

    Join Date
    Sep 2004
    Location
    Israel
    Posts
    31,135
    Rep Power
    2926
    Originally Posted by cteasen
    So here is an update...
    I was originally testing in firefox and found out that IE handles the links a little differently and will allow the saving of the full file name. Now I guess it's down to browser testing
    Any insight on why IE can save but Firefox would only try to save the first word in a file name?
    Cheers mate, this time you stumbled on bug in my original code!

    You have to change this line:
    Code:
    Response.AddHeader "Content-Disposition", "attachment; filename="&strFileName
    To this instead:
    Code:
    Response.AddHeader "Content-Disposition", "attachment; filename=""" & strFileName & """"
    This is my bad, header values should also come wrapped in quotes exactly for the case of blank space in the value - some browsers might "forgive" such thing, but some won't forgive.

    Updated the original post as well, thanks again and happy programming!
  26. #89
  27. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0
    Originally Posted by Shadow Wizard
    Cheers mate, this time you stumbled on bug in my original code!

    You have to change this line:
    Code:
    Response.AddHeader "Content-Disposition", "attachment; filename="&strFileName
    To this instead:
    Code:
    Response.AddHeader "Content-Disposition", "attachment; filename=""" & strFileName & """"
    This is my bad, header values should also come wrapped in quotes exactly for the case of blank space in the value - some browsers might "forgive" such thing, but some won't forgive.

    Updated the original post as well, thanks again and happy programming!
    Thanks everything works!
  28. #90
  29. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Jul 2013
    Posts
    1
    Rep Power
    0
    Hello I am new to the forums and have used this code for some large data file download of over 60 MB of text.
    I am having an issue when saving the file it adds the following lines at the end of the file.
    ---------------------------------------
    <font face="Arial" size=2>
    <p>Response object</font> <font face="Arial" size=2>error 'ASP 0159 : 80004005'</font>
    <p>
    <font face="Arial" size=2>Buffering Off</font>
    <p>
    <font face="Arial" size=2>/DownloadManager.asp</font><font face="Arial" size=2>, line 119</font>
    <p>
    <font face="Arial" size=2>Buffering must be on.
    </font>
    --------------------------------

    If I change the option to True, then the file name is changed and if I click save it just runs with no result.

    These are large data files with over 100000 records in them. So any help would be appreciated.

    Thanks.
Page 6 of 6 First ... 456
  • Jump to page:

Similar Threads

  1. File Download problems
    By clboren in forum ASP Development
    Replies: 0
    Last Post: March 15th, 2005, 05:54 PM
  2. Issue downloading large files
    By Ceuphrasie in forum ASP Development
    Replies: 13
    Last Post: March 14th, 2005, 11:33 AM
  3. Download Two Files From One Button?
    By Currie in forum .NET Development
    Replies: 1
    Last Post: February 5th, 2005, 04:56 AM
  4. Downloading files
    By Zil in forum .NET Development
    Replies: 7
    Last Post: January 8th, 2005, 09: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, 12:27 PM

IMN logo majestic logo threadwatch logo seochat tools logo