How to download the files using vbscript in classic asp

后端 未结 2 842
遥遥无期
遥遥无期 2020-12-09 07:28

I am working on Classic Asp with VBScript. I am trying to display list of files from a directory with download option. like,

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-09 07:40

    It seems you are trying to do this on the server-side using client-side scripting. Here is a better solution that uses server-side ASP to send the file. You will need to split your code over two pages.

    Your current script should be replaced with this:

     
     
     My First ASP Page  
     
     
    <% 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") 
    
    'Getting the list of Files 
    Set ObjFiles = ObjFolder.Files 
    
    'Writing Name and Path of each File to Output File 
    Response.Write("") 
    For Each ObjFile In ObjFiles 
        Response.Write("") 
    Next 
     Response.Write("
    "&ObjFile.Name & String(50 - Len(ObjFile.Name), " ")&"Download
    ") %>

    Then you need to create another script which I have called download.asp which handles the download:

    <%
    Dim objConn, strFile
    Dim intCampaignRecipientID
    
    strFile = Request.QueryString("file")
    
    If strFile <> "" Then
    
        Response.Buffer = False
        Dim objStream
        Set objStream = Server.CreateObject("ADODB.Stream")
        objStream.Type = 1 'adTypeBinary
        objStream.Open
        objStream.LoadFromFile("F:\karthik\" & strFile)
        Response.ContentType = "application/x-unknown"
        Response.Addheader "Content-Disposition", "attachment; filename=" & strFile
        Response.BinaryWrite objStream.Read
        objStream.Close
        Set objStream = Nothing
    
    End If
    %>
    

提交回复
热议问题