asp-classic – 如何使用经典asp中的vbscript下载文件
发布时间:2020-05-23 21:09:55 所属栏目:asp.Net 来源:互联网
导读:我正在使用VBScript处理Classic Asp.我正在尝试使用下载选项显示目录中的文件列表.喜欢, 当我点击下载链接时,需要下载相应的文件,因为我使用了以下代码, htmlheadtitle My First ASP Page /title/headbody%Dim fsoDim ObjFolderDim ObjOutFileDim ObjFile
|
我正在使用VBScript处理Classic 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:UsersstellentDownloads')"">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>
解决方法您似乎正在尝试使用客户端脚本在服务器端执行此操作.这是一个使用服务器端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
%> (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 所选值不适用于SelectList
- asp.net-mvc – Azure git部署 – 第二个程序集中缺少引用
- .net – .ToTitleCase不适用于所有大写字符串
- asp.net-mvc – ASP.NET MVC中的AntiForgeryToken是否防止所
- ASP.NET Dropdownlist selectedindexchanged事件未在向上/向
- asp.net-mvc – 有没有人知道让Ninject 2在ASP.NET MVC中工
- 从ASP.NET切换到面包和黄油HTML / jQuery的优势
- .NET 3.5 / VS 2008上的ASP.NET Web Services的自定义HTTP基
- asp.net-identity-2 – Asp.Net身份保存用户没有电子邮件
- .net – MVC6的VersionedRoute属性实现
推荐文章
站长推荐
- asp.net-mvc-3 – ASP.Net MVC 3剃刀:部分定义但
- asp.net-mvc – 将ASP.NET MVC布局应用于Jquery.
- asp.net-mvc – 如何在MVC中创建通用视图模型?
- asp.net – 如何在visual studio中查看cshtml页面
- 什么是ASP.NET的最佳/推荐的CAPTCHA组件
- asp.net – 如何从相同的解决方案的MVC项目调试W
- asp.net – 如何从Javascript调用控制器方法
- asp.net – 在Web应用程序中排队长时间运行的任务
- asp.net – 如何在没有文件扩展名的情况下制作AS
- asp.net-mvc – MVC4是否被烘烤到.NET 4.5?
热点阅读
