如何在传统的ASP使用VBScript下载的文件 [英] How to download the files using vbscript in classic asp

查看:91
本文介绍了如何在传统的ASP使用VBScript下载的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作经典ASP使用VBScript。我试图从下载选项目录显示文件列表。喜欢,

当我下载链接点击相应的文件必须下载,我使用了下列code类的,

 < HTML和GT;
< HEAD>
<标题>我的第一个ASP页< /标题>
< /头>
<身体GT;
<%
昏暗的FSO
昏暗ObjFolder
昏暗ObjOutFile
昏暗ObjFiles
昏暗OBJFILE创建文件系统对象
FSO设置=的CreateObject(Scripting.FileSystemObject的)获取文件夹对象
设置ObjFolder = fso.GetFolder(F:\\ KARTHIK)创建一个输出文件写的文件名
设置ObjOutFile = fso.CreateTextFile(F:\\ WindowsFiles.txt)获取的文件列表
设置ObjFiles = ObjFolder.Files写作的名称和每个文件输出文件的路径
回复于(&下;表CELLPADDING =4CELLSPACING =5>中)
对于每个OBJFILE在ObjFiles
    回复于(&所述; TR&GT;&下; TD&gt;中和放大器; ObjFile.Name&放大器;字符串(50 - 莱恩(ObjFile.Name),)及&下; / TD&GT;&下; TD&GT;&下;一个HREF =#LANGUAGE =VBSCRIPT onclick=\"\"vbscript:HTTPDownload('\"&ObjFile.Path&\"','C:\\Users\\stellent\\Downloads\\')\"\">Download</a></td></tr>\")
下一个
 的Response.Write(&LT; /表&gt;)
ObjOutFile.Close
%GT;&LT; BR&GT;
&LT; SCRIPT LANGUAGE =VBSCRIPT类型=文/ VBScript中&GT;
子HTTPDownload(myURL,mypath中)
    标准看家
    我朦胧,OBJFILE,objFSO,objHTTP,strFile,strMsg
    常量ForReading的= 1,ForWriting = 2,ForAppending = 8    '创建一个文件系统对象
    设置objFSO =的CreateObject(Scripting.FileSystemObject的)    检查指定的目标文件或文件夹存在,
    并建立目标文件的完全限定路径
    如果objFSO.FolderExists(mypath中)然后
        strFile = objFSO.BuildPath(mypath中,MID(myURL,InStrRev(myURL,/)+ 1))
    elseif的objFSO.FolderExists(左(mypath中,InStrRev(mypath中,\\) - 1))然后
        strFile = mypath中
    其他
        WScript.Echo错误:目标文件夹未找到。
        退出小组
    万一    创建或打开目标文件
    设置OBJFILE = objFSO.OpenTextFile(strFile,ForWriting,真)    创建一个HTTP对象
    设置objHTTP =的CreateObject(WinHttp.WinHtt prequest.5.1)    下载指定的URL
    objHTTP.OpenGET,myURL,假
    objHTTP.Send    '编写下载的字节流以目标文件
    对于i = 1到LENB(objHTTP.ResponseBody)
        objFile.Write CHR(ASCB(MIDB(objHTTP.ResponseBody,I,1)))
    下一个    '关闭目标文件
    objFile.Close()
结束小组
&LT; / SCRIPT&GT;
&LT; /身体GT;
&LT; / HTML&GT;


解决方案

您似乎正在尝试做这个使用客户端脚本服务器端。下面是使用服务器端ASP文件发送一个更好的解决方案。您需要在两页拆你的code。

您当前的脚本应该以这个来代替:

 &LT; HTML和GT;
&LT; HEAD&GT;
&LT;标题&GT;我的第一个ASP页&LT; /标题&GT;
&LT; /头&GT;
&LT;身体GT;
&LT;%FSO暗淡
昏暗ObjFolder
昏暗ObjOutFile
昏暗ObjFiles
昏暗OBJFILE创建文件系统对象
FSO设置=的CreateObject(Scripting.FileSystemObject的)获取文件夹对象
设置ObjFolder = fso.GetFolder(F:\\ KARTHIK)获取的文件列表
设置ObjFiles = ObjFolder.Files写作的名称和每个文件输出文件的路径
回复于(&下;表CELLPADDING =4CELLSPACING =5&gt;中)
对于每个OBJFILE在ObjFiles
    回复于(&所述; TR&GT;&下; TD&gt;中和放大器; ObjFile.Name&放大器;字符串(50 - 莱恩(ObjFile.Name),)及&下; / TD&GT;&下; TD&GT;&下;一个HREF =download.asp文件=?&放大器; Server.UrlEn code(ObjFile.Name)及&GT;下载&LT; / A&GT;&LT; / TD&GT;&LT; / TR&gt;中)
下一个
 的Response.Write(&LT; /表&gt;)
%GT;&LT; BR&GT;
&LT; /身体GT;
&LT; / HTML&GT;

然后你需要创建另一个脚本,我称之为 download.asp 负责处理下载:

 &LT;%
暗淡objConn,strFile
昏暗intCampaignRecipientIDstrFile =的Request.QueryString(文件)如果strFile&LT;&GT; 然后    将Response.Buffer = FALSE
    昏暗objStream
    设置objStream =的Server.CreateObject(的ADODB.Stream)
    objStream.Type = 1'adTypeBinary
    objStream.Open
    objStream.LoadFromFile(F:\\ KARTHIK \\&放大器; strFile)
    Response.ContentType =应用程序/ x-未知
    Response.Addheader内容处置,附件;文件名=&放大器; strFile
    Response.BinaryWrite objStream.Read
    objStream.Close
    设置objStream =什么万一
%GT;

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

When i click on the download link the corresponding file need to be download for that i have used the following code like,

<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>

解决方案

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:

<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>

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

这篇关于如何在传统的ASP使用VBScript下载的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆