Connecting Tech Pros Worldwide Help | Site Map

Downloading with ASP

colinod's Avatar
Familiar Sight
 
Join Date: Nov 2007
Posts: 182
#1: Jun 18 '09
I am using the following code to make a button that downloads files from my server, the problem is that once the file size gets over 3mb it wont download and just hangs, is this a problem with the code???

Expand|Select|Wrap|Line Numbers
  1. <%@Language="VBScript"%>
  2. <%Option Explicit%>
  3. <%Response.Buffer = True%>
  4. <%
  5. On Error Resume Next
  6. Dim strPath
  7. strPath = CStr(Request.QueryString("file"))
  8. '-- do some basic error checking for the QueryString
  9. If strPath = "" Then
  10.     Response.Clear
  11.     Response.Write("No file specified.")
  12.     Response.End
  13. ElseIf InStr(strPath, "..") > 0 Then
  14.     Response.Clear
  15.     Response.Write("Illegal folder location.")
  16.     Response.End
  17. ElseIf Len(strPath) > 1024 Then
  18.     Response.Clear
  19.     Response.Write("Folder path too long.")
  20.     Response.End
  21. Else
  22.     Call DownloadFile(strPath)
  23. End If
  24.  
  25. Private Sub DownloadFile(file)
  26.     '--declare variables
  27.     Dim strAbsFile
  28.     Dim strFileExtension
  29.     Dim objFSO
  30.     Dim objFile
  31.     Dim objStream
  32.     '-- set absolute file location
  33.     strAbsFile = Server.MapPath(file)
  34.     '-- create FSO object to check if file exists and get properties
  35.     Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
  36.     '-- check to see if the file exists
  37.     If objFSO.FileExists(strAbsFile) Then
  38.         Set objFile = objFSO.GetFile(strAbsFile)
  39.         '-- first clear the response, and then set the appropriate headers
  40.         Response.Clear
  41.         '-- the filename you give it will be the one that is shown
  42.         ' to the users by default when they save
  43.         Response.AddHeader "Content-Disposition", "attachment; filename=" & objFile.Name
  44.         Response.AddHeader "Content-Length", objFile.Size
  45.         Response.ContentType = "application/octet-stream"
  46.         Set objStream = Server.CreateObject("ADODB.Stream")
  47.         objStream.Open
  48.         '-- set as binary
  49.         objStream.Type = 1
  50.         Response.CharSet = "UTF-8"
  51.         '-- load into the stream the file
  52.         objStream.LoadFromFile(strAbsFile)
  53.         '-- send the stream in the response
  54.         Response.BinaryWrite(objStream.Read)
  55.         objStream.Close
  56.         Set objStream = Nothing
  57.         Set objFile = Nothing
  58.     Else 'objFSO.FileExists(strAbsFile)
  59.         Response.Clear
  60.         Response.Write("No such file exists.")
  61.     End If
  62.     Set objFSO = Nothing
  63. End Sub
  64. %>
Reply