Error in downloaded pdf file - ASP classic

依然范特西╮ 提交于 2019-12-25 05:24:08

问题


I'm creating a download manager, and everything seems to work. But the file that I get downloaded can not be opened. The file has the correct name and the extension .pdf - but my Mac says that the file cannot be opened (the file on the server works).

if request.querystring("downloadFile") <> "" then

    strFilePath = Server.MapPath("/_customerFiles/"& session("URLmapping") &"/documents/"& request.querystring("downloadFile"))

    Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

    If objFSO.FileExists(strFilePath) Then
        Set objFile = objFSO.GetFile(strFilePath)
        intFileSize = objFile.Size
        Set objFile = Nothing

        strFileName = request.querystring("filename")
        strFileName = replace(request.querystring("downloadFile")," ","-")

        Response.AddHeader "Content-Disposition","attachment; filename=" & strFileName
        Response.ContentType = "application/octet-stream"
        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

    End if

    Set objFSO = Nothing

end if

回答1:


Have you considered that the output stream is having a Byte Order Mark (BOM) added (or maybe removed)? I would open both files in a hex editor and look for 3 bytes added at the start (and everything else moved along).




回答2:


I know I'm late getting to this question, but I was just dealing with something similar and wanted share my observations to try to clear it up.

  1. I have read (sorry I dont have a source) that the ADODB.Stream has to have its .Type specified prior to calling .Open
  2. I also set the Stream .Mode property to 3 (meaning "open file for Reading") to prevent file access lock errors if multiple people try to access the file at the same time -- Also must be done before calling .Open
  3. I have had varying success with Response.ContentType "application/octet-stream", and after some problems there, I changed mine to Response.ContentType "xxx/xxx". The browser will accept it as an unknown file type and then prompt the user to open it based on the registered program for that file extension.
  4. Before you start adding Response Headers, you should call Response.Clear (just to be sure you're not sending extra markup)
  5. After closing your FileSystemObject, you should call Response.End (again, just to be sure)



回答3:


You needs to add the Response.Clear before Response.AddHeader and change the Response.ContentType, so the code is like bellow:

if request.querystring("downloadFile") <> "" then

strFilePath = Server.MapPath("/_customerFiles/"& session("URLmapping") &"/documents/"& request.querystring("downloadFile"))

Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

If objFSO.FileExists(strFilePath) Then
    Set objFile = objFSO.GetFile(strFilePath)
    intFileSize = objFile.Size
    Set objFile = Nothing

    strFileName = request.querystring("filename")
    strFileName = replace(request.querystring("downloadFile")," ","-")

    Response.Clear
    Response.AddHeader "Content-Disposition","attachment; filename=" & strFileName
    Response.ContentType = "xxx/xxx"
    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

End if

Set objFSO = Nothing

End if


来源:https://stackoverflow.com/questions/17828299/error-in-downloaded-pdf-file-asp-classic

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!