经典ASP下载PDF页面加载

时间:2014-09-12 12:09:53

标签: vbscript asp-classic

我想创建一个可以显示消息的页面"您的下载即将开始"然后在几秒钟之后打开一个"另存为"允许访问者下载文件的对话。这在Classic ASP VB脚本中是否可行?我知道如何将页面流设置为文件,但它不显示页面的html。我提供的文件是20Mb,因此脚本需要处理大文件。

我目前有一个元重定向:

<meta http-equiv="refresh" content="2; url=/downloads/brochures/ACET_Products_and_Services_Directory_2013-14.pdf" />

但这并不是什么好事。

我在我的服务器上安装了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"
%>

这可以绕过大文件,但您无法同时传输文件并显示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 
%>

有了这个,我从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
%>

然后我使用&lt;%response.redirect(&#34; download.asp&#34;)%&gt;在我希望它下载的页面上,但是当我点击页面时,我得到了文件,但没有页面。这部分我正在努力。

SUCCESS!

<script> 
window.location.replace('download.asp'); 
</script>

干杯,

史蒂夫

1 个答案:

答案 0 :(得分:3)

通过更多的试验和错误,我发现创建了一个名为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
%>

然后我将此代码放在我想要显示说明的页面上,然后提供自动下载:

<script> 
window.location.replace('download.asp'); 
</script>

我希望其他人觉得这很有用。

史蒂夫