ADODB Stream:下载文本文件问题

时间:2017-02-22 19:52:35

标签: asp-classic stream adodb

我处于用户点击下载按钮时调用流连接的情况。提交下载按钮时,将在ASP中调用一个函数,其中下载代码在文件BD_Test1.txt上执行。此文本文件的内容只是TEST 1

文件正常下载,但打开时,所有HTML代码都在文本文件中。

HTML代码

<html>

<head>
    <title>Bulk Download Zipper</title>
    <!--#include file="BD_Download.asp"-->
</head>

<body>
    <center><h1>Bulk Download Zipper</h1></center>

    <br><br>

    <p>Please click "Download" to zip the files.</p>

    <form method="post">
        <br><input type = "submit" name="zipFile" value = "Download"/><br>
    </form>

    <br><br>

    <%
    If (Request.Form("zipFile") <> "") Then
        Call downloadFile()
    End If
    %>
</body>

</html>

ASP代码

<%
Function downloadFile()
    Dim download_File, download_Remove, download_Autoremove

    Set download_File = Server.CreateObject("ADODB.Stream")
    download_File.Type = 1
    download_File.Open
    download_File.LoadFromFile("E:\inetpub\wwwroot\cdc\wnyaccc_erie_niagara_trd-2\youkergav\BulkDownload\resumes\BD_Test1.txt")

    Response.AddHeader "Content-Transfer-Encoding", "binary" 
    Response.AddHeader "Content-Description", "File Transfer" 
    Response.AddHeader "Content-Disposition", "attachment; filename=Resumes.txt"
    Response.CharSet = "UTF-8"
    Response.ContentType = "text/plain"
    Response.BinaryWrite download_File.Read

    download_File.Close
    Set download_File = Nothing
End Function
%>

下载的文本文件的内容

<html>

<head>
    <title>Bulk Download Zipper</title>

</head>

<body>
    <center><h1>Bulk Download Zipper</h1></center>

    <br><br>

    <p>Please click "Download" to zip the files.</p>

    <form method="post">
        <br><input type = "submit" name="zipFile" value = "Download"/><br>
    </form>

    <br><br>

    TEST 1
</body>

</html>

如您所见,正在插入文本文件的内容,其中调用了downloadFile()函数。是否有可能以这种方式使用流连接?

1 个答案:

答案 0 :(得分:2)

在页面已经开始提供给客户端后,您的downloadFile功能被称为。 ASP只是简单地注入你的函数的结果。您需要在POST上覆盖整个输出。在GET上输出html,并在POST上输出文本文件:

<%
If (Request.Form("zipFile") <> "") Then
    Call downloadFile()
Else%>
    <html>

    <head>
        <title>Bulk Download Zipper</title>
        <!--#include file="BD_Download.asp"-->
    </head>

    <body>
        <center><h1>Bulk Download Zipper</h1></center>

        <br><br>

        <p>Please click "Download" to zip the files.</p>

        <form method="post">
            <br><input type = "submit" name="zipFile" value = "Download"/><br>
        </form>

        <br><br>
    </body>

    </html>
<%
End If
%>