title: How to download the files using vbscript in classic asp
body: <p>I am working on Classic Asp with VBScript. I am trying to display list of files from a directory with download option. like,<img src="https://i.stack.imgur.com/v6Vdv.png" alt="enter image description here"></p>
  
  <p>When i click on the download link the corresponding file need to be download for that i have used the following code like,</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;title&gt; My First ASP Page &lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;%
  Dim fso
  Dim ObjFolder
  Dim ObjOutFile
  Dim ObjFiles
  Dim ObjFile
  
  'Creating File System Object
  Set fso = CreateObject("Scripting.FileSystemObject")
  
  'Getting the Folder Object
  Set ObjFolder = fso.GetFolder("F:\karthik")
  
  'Creating an Output File to write the File Names
  Set ObjOutFile = fso.CreateTextFile("F:\WindowsFiles.txt")
  
  'Getting the list of Files
  Set ObjFiles = ObjFolder.Files
  
  'Writing Name and Path of each File to Output File
  Response.Write("&lt;table cellpadding=""4"" cellspacing=""5"" &gt;")
  For Each ObjFile In ObjFiles
      Response.Write("&lt;tr&gt;&lt;td&gt;"&amp;ObjFile.Name &amp; String(50 - Len(ObjFile.Name), " ")&amp;"&lt;/td&gt;&lt;td&gt;&lt;a href=""#"" language=""VBScript"" onclick=""vbscript:HTTPDownload('"&amp;ObjFile.Path&amp;"','C:\Users\stellent\Downloads\')""&gt;Download&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;")
  Next
   Response.Write("&lt;/table&gt;")
  ObjOutFile.Close
  %&gt;&lt;br&gt;
  &lt;script language="vbscript" type="text/vbscript"&gt;
  Sub HTTPDownload( myURL, myPath )
      ' Standard housekeeping
      Dim i, objFile, objFSO, objHTTP, strFile, strMsg
      Const ForReading = 1, ForWriting = 2, ForAppending = 8
  
      ' Create a File System Object
      Set objFSO = CreateObject( "Scripting.FileSystemObject" )
  
      ' Check if the specified target file or folder exists,
      ' and build the fully qualified path of the target file
      If objFSO.FolderExists( myPath ) Then
          strFile = objFSO.BuildPath( myPath, Mid( myURL, InStrRev( myURL, "/" ) + 1 ) )
      ElseIf objFSO.FolderExists( Left( myPath, InStrRev( myPath, "\" ) - 1 ) ) Then
          strFile = myPath
      Else
          WScript.Echo "ERROR: Target folder not found."
          Exit Sub
      End If
  
      ' Create or open the target file
      Set objFile = objFSO.OpenTextFile( strFile, ForWriting, True )
  
      ' Create an HTTP object
      Set objHTTP = CreateObject( "WinHttp.WinHttpRequest.5.1" )
  
      ' Download the specified URL
      objHTTP.Open "GET", myURL, False
      objHTTP.Send
  
      ' Write the downloaded byte stream to the target file
      For i = 1 To LenB( objHTTP.ResponseBody )
          objFile.Write Chr( AscB( MidB( objHTTP.ResponseBody, i, 1 ) ) )
      Next
  
      ' Close the target file
      objFile.Close( )
  End Sub
  &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>