【问题标题】:Classic ASP code to download .CSV file用于下载 .CSV 文件的经典 ASP 代码
【发布时间】:2014-02-15 23:53:15
【问题描述】:

我有一个用经典 ASP 编写的网站 一个页面从 SQL 数据库创建一个 .CSV 文件并将其与“单击此处”链接一起存储在根目录中以将其下载到用户 PC

多年来一直运行良好,在下载小文件时仍然运行良好,但现在在下载 .csv 文件(在此示例中)时出现“无法显示此页”错误约 785 条记录

代码很短,如下所示,只有一个 Private Sub 负责下载

<%@Language="VBScript"%>

<%Reponse.Buffer = True%>
<%
On Error Resume Next
Dim strPath
strPath = CStr(Request.QueryString("File"))
'-- do some basic error checking for the QueryString
If strPath = "" Then
    Response.Clear
Response.Write("No file specified.")
Response.End
ElseIf InStr(strPath, "..") > 0 Then
Response.Clear
Response.Write("Illegal folder location.")
Response.End
ElseIf Len(strPath) > 1024 Then
Response.Clear
Response.Write("Folder path too long.")
Response.End
Else
Call DownloadFile(strPath)
End If

Private Sub DownloadFile(file)
'--declare variables
Dim strAbsFile
Dim strFileExtension
Dim objFSO
Dim objFile
Dim objStream
'-- set absolute file location
strAbsFile = Server.MapPath(file)
'-- create FSO object to check if file exists and get properties
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
'-- check to see if the file exists
If objFSO.FileExists(strAbsFile) Then
    Set objFile = objFSO.GetFile(strAbsFile)
    '-- first clear the response, and then set the appropriate headers
    Response.Clear
    '-- the filename you give it will be the one that is shown
    ' to the users by default when they save
    Response.AddHeader "Content-Disposition", "attachment; filename=" & objFile.Name
    Response.AddHeader "Content-Length", objFile.Size
    Response.ContentType = "application/octet-stream"
    Set objStream = Server.CreateObject("ADODB.Stream")
    objStream.Open
    '-- set as binary
    objStream.Type = 1
    Response.CharSet = "UTF-8"
    '-- load into the stream the file
    objStream.LoadFromFile(strAbsFile)
    '-- send the stream in the response
    Response.BinaryWrite(objStream.Read)
    objStream.Close
    Set objStream = Nothing
    Set objFile = Nothing
Else 'objFSO.FileExists(strAbsFile)
    Response.Clear
    Response.Write("No such file exists.")
End If
Set objFSO = Nothing
End Sub
%>

所以最近几个月发生了一些变化

非常感谢您的建议

提前致谢

【问题讨论】:

  • 将检查您的服务器配置是否已更改。有没有升级之类的?您要检查的值是AspMaxRequestEntityAllowed,默认为 200 KB,因此这可能解释了为什么有些工作而其他人不工作,默认情况下超过 200 KB 的任何东西都会失败。如何配置取决于您运行的 IIS 版本。

标签: asp-classic download


【解决方案1】:

可能需要您的 IIS 版本和配置来提供帮助,但我知道 IIS6 对 BinaryWrite 引入了 4MB 的限制。请尝试分块发送文件。

<% Response.Buffer = False %>
...
With Server.CreateObject("ADODB.Stream")
    .Open
    .Type = 1
    .LoadFromFile strPath

    ' Send 256K chunks...
    Const intChunkSize = 262144

    Do While Response.IsClientConnected And (.Position < .Size)
        Response.BinaryWrite .Read(intChunkSize)
    Loop

    .Close
End With

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 2017-10-30
    相关资源
    最近更新 更多