【问题标题】:Download an excel file classic asp下载一个excel文件经典asp
【发布时间】:2014-09-13 06:43:40
【问题描述】:

我正在尝试通过单击按钮下载一个 excel 文件(保存在服务器上)。引发事件时,它会下载一个空白的 excel 文件而不是“WORK.xls”

 <%
     strFile = "WORK.xls"

     Response.ContentType = "application/octet-stream"
     Response.AddHeader "Content-Disposition", "attachment; filename=" & strFile

     set app = Server.CreateObject("ADODB.Stream")
     app.open
     app.type = adTypeBinary
     app.LoadFromFile(Server.MapPath("WORK.xls"))

    response.binarywrite app.Read

    app.close

   Set app = nothing

 %>

【问题讨论】:

  • 您遇到了什么问题?
  • 一个空白的excel文件被下载,而不是服务器上保存的文件“WORK.xls”

标签: excel asp-classic


【解决方案1】:

试试这个:

DownloadFile "WORK.xls"

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

链接:http://www.evagoras.com/2011/02/08/downloading-any-file-using-asp-fso-and-the-adodb-stream-object/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-01
    • 1970-01-01
    • 2015-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多