title: Classic ASP code to download .CSV file
body: <p>I have a website written in Classic ASP
  One page creates a .CSV file from a SQL database and stores it in the root directory along with a "Click Here" link to download it to the users PC</p>
  
  <p>It has been working fine for many years and is still working fine when downloading small files, but now it comes up with a "THIS PAGE CAN'T BE DISPLAYED" error when downloading a .csv file (in this example) of some 785 records</p>
  
  <p>The code is pretty short as below with the one Private Sub that does the download</p>
  
  <pre><code>&lt;%@Language="VBScript"%&gt;
  
  &lt;%Reponse.Buffer = True%&gt;
  &lt;%
  On Error Resume Next
  Dim strPath
  strPath = CStr(Request.QueryString("File"))
  '-- do some basic error checking for the QueryString
  If strPath = "" Then
      Response.Clear
  Response.Write("No file specified.")
  Response.End
  ElseIf InStr(strPath, "..") &gt; 0 Then
  Response.Clear
  Response.Write("Illegal folder location.")
  Response.End
  ElseIf Len(strPath) &gt; 1024 Then
  Response.Clear
  Response.Write("Folder path too long.")
  Response.End
  Else
  Call DownloadFile(strPath)
  End If
  
  Private Sub DownloadFile(file)
  '--declare variables
  Dim strAbsFile
  Dim strFileExtension
  Dim objFSO
  Dim objFile
  Dim objStream
  '-- set absolute file location
  strAbsFile = Server.MapPath(file)
  '-- create FSO object to check if file exists and get properties
  Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
  '-- check to see if the file exists
  If objFSO.FileExists(strAbsFile) Then
      Set objFile = objFSO.GetFile(strAbsFile)
      '-- first clear the response, and then set the appropriate headers
      Response.Clear
      '-- the filename you give it will be the one that is shown
      ' to the users by default when they save
      Response.AddHeader "Content-Disposition", "attachment; filename=" &amp; objFile.Name
      Response.AddHeader "Content-Length", objFile.Size
      Response.ContentType = "application/octet-stream"
      Set objStream = Server.CreateObject("ADODB.Stream")
      objStream.Open
      '-- set as binary
      objStream.Type = 1
      Response.CharSet = "UTF-8"
      '-- load into the stream the file
      objStream.LoadFromFile(strAbsFile)
      '-- send the stream in the response
      Response.BinaryWrite(objStream.Read)
      objStream.Close
      Set objStream = Nothing
      Set objFile = Nothing
  Else 'objFSO.FileExists(strAbsFile)
      Response.Clear
      Response.Write("No such file exists.")
  End If
  Set objFSO = Nothing
  End Sub
  %&gt;
  </code></pre>
  
  <p>So something has changed in recent months</p>
  
  <p>Ay advice much appreciated</p>
  
  <p>Thanks in advance</p>