经典ASP代码下载.CSV文件

时间:2014-02-15 01:34:52

标签: asp-classic download

我有一个用经典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
%>

近几个月发生了一些变化

Ay建议非常感谢

提前致谢

1 个答案:

答案 0 :(得分:0)

您的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