Ste*_*CET 0 vbscript asp-classic
我想创建一个页面,该页面可以显示一条消息“您的下载即将开始”,然后几秒钟后打开一个“另存为”对话框,允许访问者下载文件。这在经典 ASP VB 脚本中是可能的吗?我知道如何使页面流成为文件,但它不显示页面的 html。我提供的文件是 20Mb,所以脚本需要处理大文件。
我目前有一个元重定向:
<meta http-equiv="refresh" content="2; url=/downloads/brochures/ACET_Products_and_Services_Directory_2013-14.pdf" />
Run Code Online (Sandbox Code Playgroud)
但这真的不是什么好事。
我在我的服务器上安装了 asppdf,并试了一下:
<%
Set Pdf = Server.CreateObject("Persits.Pdf")
Set Doc = Pdf.OpenDocument("d:/websites/common/downloads/brochures/ACET_Products_and_Services_Directory_2013-14.pdf")
Doc.SaveHttp "attachment;filename=ACET_Products_and_Services_Directory_2013-14.pdf"
%>
Run Code Online (Sandbox Code Playgroud)
这绕过了大文件,但您不能同时传输文件和显示 HTML。
我找到了很多方法将文件流式传输到浏览器,但是在页面显示后我无法这样做。
这是我尝试过的另一种:
<%
Response.Buffer = False
Server.ScriptTimeout = 30000
Response.ContentType = "application/x-unknown" ' arbitrary
fn = "ACET_Products_and_Services_Directory_2013-14.pdf"
FPath = "d:\websites\common\downloads\brochures\" & fn
Response.AddHeader "Content-Disposition", "attachment; filename=" & fn
Set adoStream = CreateObject("ADODB.Stream")
chunk = 2048
adoStream.Open()
adoStream.Type = 1
adoStream.LoadFromFile(FPath)
iSz = adoStream.Size
Response.AddHeader "Content-Length", iSz
For i = 1 To iSz \ chunk
If Not Response.IsClientConnected Then Exit For
Response.BinaryWrite adoStream.Read(chunk)
Next
If iSz Mod chunk > 0 Then
If Response.IsClientConnected Then
Response.BinaryWrite adoStream.Read(iSz Mod chunk)
End If
End If
adoStream.Close
Set adoStream = Nothing
Response.End
%>
Run Code Online (Sandbox Code Playgroud)
有了这个,我得到了一个错误代码:来自 Chrome 的 ERR_INVALID_RESPONSE。
这是我尝试过的几乎有效的方法:
<%
strFilePath = "d:/web sites/common/downloads/brochures/ACET_Products_and_Services_Directory_2013-14.pdf"
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strFilePath) Then
Set objFile = objFSO.GetFile(strFilePath)
intFileSize = objFile.Size
Set objFile = Nothing
strFileName = "ACET_Products_and_Services_Directory_2013-14.pdf"
Response.AddHeader "Content-Disposition","attachment; filename=" & strFileName
Response.ContentType = "application/x-msdownload"
Response.AddHeader "Content-Length", intFileSize
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 1 'adTypeBinary
objStream.LoadFromFile strFilePath
Do While Not objStream.EOS And Response.IsClientConnected
Response.BinaryWrite objStream.Read(1024)
Response.Flush()
Loop
objStream.Close
Set objStream = Nothing
Else
Response.write "Error finding file."
End if
Set objFSO = Nothing
%>
Run Code Online (Sandbox Code Playgroud)
然后我在我希望它下载的页面上使用了 <% response.redirect("download.asp") %> ,但是一旦我点击页面,我就得到了文件,但没有页面。这是我正在努力解决的部分。
成功!
<script>
window.location.replace('download.asp');
</script>
Run Code Online (Sandbox Code Playgroud)
干杯,
史蒂夫
通过更多的试验和错误,我发现创建了一个名为 download.asp 的文件并将此代码放入工作中:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
strFilePath = "d:/websites/common/downloads/brochures/ACET_Products_and_Services_Directory_2013-14.pdf"
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strFilePath) Then
Set objFile = objFSO.GetFile(strFilePath)
intFileSize = objFile.Size
Set objFile = Nothing
strFileName = "ACET_Products_and_Services_Directory_2013-14.pdf"
Response.AddHeader "Content-Disposition","attachment; filename=" & strFileName
Response.ContentType = "application/pdf"
Response.AddHeader "Content-Length", intFileSize
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 1 'adTypeBinary
objStream.LoadFromFile strFilePath
Do While Not objStream.EOS And Response.IsClientConnected
Response.BinaryWrite objStream.Read(1024)
Response.Flush()
Loop
objStream.Close
Set objStream = Nothing
Else
Response.write "Error finding file."
End if
Set objFSO = Nothing
%>
Run Code Online (Sandbox Code Playgroud)
然后我将此代码放在我想要显示说明的页面上,然后提供自动下载:
<script>
window.location.replace('download.asp');
</script>
Run Code Online (Sandbox Code Playgroud)
我希望其他人觉得这很有用。
史蒂夫
| 归档时间: |
|
| 查看次数: |
7665 次 |
| 最近记录: |