2012-10-17 69 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