2012-10-17 91 views
2

我正在使用VBScript處理Classic Asp。我正在嘗試使用下載選項顯示目錄中的文件列表。像,enter image description here如何使用vbscript在經典asp中下載文件

當我點擊鏈接下載相應的文件必須下載,我是用下面的代碼一樣,

<html> 
<head> 
<title> My First ASP Page </title> 
</head> 
<body> 
<% 
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("<table cellpadding=""4"" cellspacing=""5"" >") 
For Each ObjFile In ObjFiles 
    Response.Write("<tr><td>"&ObjFile.Name & String(50 - Len(ObjFile.Name), " ")&"</td><td><a href=""#"" language=""VBScript"" onclick=""vbscript:HTTPDownload('"&ObjFile.Path&"','C:\Users\stellent\Downloads\')"">Download</a></td></tr>") 
Next 
Response.Write("</table>") 
ObjOutFile.Close 
%><br> 
<script language="vbscript" type="text/vbscript"> 
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 
</script> 
</body> 
</html> 
+0

..和怎麼了 ?下載是否發生? – SearchAndResQ

+0

是的..沒有下載發生。我認爲我的錨標記不會調用vbscript方法。 –

+0

這是一個asp文件,不是vbs。你將不得不把代碼放在asp標籤的腳本塊中,然後嘗試。該鏈接將不得不做回發並調用函數來下載 – SearchAndResQ

回答

9

似乎你試圖做到這一點的服務器 - 使用客戶端腳本。這是一個更好的解決方案,它使用服務器端ASP來發送文件。您需要將代碼分成兩頁。

您當前的腳本應該以這個來代替:

<html> 
<head> 
<title> My First ASP Page </title> 
</head> 
<body> 
<% 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("<table cellpadding=""4"" cellspacing=""5"" >") 
For Each ObjFile In ObjFiles 
    Response.Write("<tr><td>"&ObjFile.Name & String(50 - Len(ObjFile.Name), " ")&"</td><td><a href=""download.asp?file=" & Server.UrlEncode(ObjFile.Name) & """>Download</a></td></tr>") 
Next 
Response.Write("</table>") 
%><br> 
</body> 
</html> 

然後,你需要創建另一個腳本,我呼籲download.asp它處理下載:

<% 
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 
%> 
+0

謝謝約翰,我被困在類似的情況,但它的固定現在! – yaqoob

+1

請注意這種方法。沒有什麼能阻止用戶在查詢字符串中提供他們自己的文件名,並讓你的腳本爲他們下載它。小心父路徑請求:例如'.. \ .. \ global.asa'。 – Bond

+0

好的建議@Bond - 至少應該拒絕任何請求,其中'file'包含反斜槓。 – johna