How to download the files using vbscript in classic asp

后端 未结 2 841
遥遥无期
遥遥无期 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:52

    I like this solution, but users can see the downloads in the history, or modify the querystring. This solution can be modified for POST usage this way: in the page code modify the link: FileName` and further down

        

    then in your javascript file get the filename:

        function getfile(obj) {
          var f=obj.innerText;
          $("#frm2dl #file2dl").val(f);
          $("#frm2dl").submit();
        }
    

    alternately you could use a file ID then in the download.asp have a lookup function from ID to filename. Then in the download.asp use request.form("file2dl") instead of request.querystring.

    UPDATE: Also, depending on server version you might get the 4MB limit (I have to work with Microsoft-IIS/7.5 on intranet). Therefore for large files the code will not work. Here is my improved version:

    Dim strFileName, strFilePath, objFSO, objStream, objFile, intFileSize
    Const lChkSize = 524288 ' 500KB - server typical limit is 4MB 
    'If session("loggedIn") = True Then ' insert your logon validation code here. bypassed for testing
        strFileName = request.form("file2dl")
        strFilename = Replace(strFilename,"..","") ' prevent parent path navigation - also ensure uploaded files do not contain this sequence
        strFilename = Replace(strFilename,"/","") ' prevent path navigation
        strFilename = Replace(strFilename,"\","") ' filenames should already be cleaned by a previous process
        strFilePath = server.MapPath("/insert your URL absolute sources filepath here/" & strFilename)
        Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
        If objFSO.FileExists(strFilePath) Then
            Set objFile = objFSO.GetFile(strFilePath)
            intFileSize = objFile.Size
            Set objFile = Nothing
            Response.AddHeader "Content-Disposition","attachment; filename=" & strFileName
            Response.ContentType = "application/x-msdownload"
            Response.AddHeader "Content-Length", intFileSize
            Set objStream = Server.CreateObject("ADODB.Stream")
            objStream.Type = 1 'adTypeBinary
            objStream.Open
            objStream.LoadFromFile strFilePath
            Do While Not objStream.EOS And Response.IsClientConnected
                Response.BinaryWrite objStream.Read(lChkSize)
                Response.Flush()
            Loop
            objStream.Close
            Set objStream = Nothing
        Else
            Response.write "Error finding file: " & request.form("file2dl")
        End if
        Set objFSO = Nothing
    'End If
    

提交回复
热议问题