开发者

PDF returning a corrupt file

开发者 https://www.devze.com 2022-12-13 21:14 出处:网络
I am using the following bit of code to send a pdf file back to the user. this works fine on my pc and on all of our test pcs. however the user is complaining the document is corrupt. When I look at t

I am using the following bit of code to send a pdf file back to the user. this works fine on my pc and on all of our test pcs. however the user is complaining the document is corrupt. When I look at the pdf file sent back in notepad I can see some HTML after the binary infomation.

protected void btnGetFile_Click(object sender, EventArgs e)
        {
            string title = "DischargeSummary.pdf";
          开发者_如何学JAVA  string contentType = "application/pdf";
            byte[] documentBytes = GetDoc(DocID);

            Response.Clear();
            Response.ContentType = contentType;
            Response.AddHeader("content-disposition", "attachment;filename=" + title);
            Response.BinaryWrite(documentBytes);
        }


The issue is cause by the response object appending to the end of the file bytes the Parsed HTML for the page at the end of the file. This can be prevented by calling Response.Close(), after you

have written the file to the buffer.

protected void btnGetFile_Click(object sender, EventArgs e)
        {
            string title = "DischargeSummary.pdf";
            string contentType = "application/pdf";
            byte[] documentBytes = GetDoc(DocID);

            Response.Clear();
            Response.ContentType = contentType;
            Response.AddHeader("content-disposition", "attachment;filename=" + title);
            Response.BinaryWrite(documentBytes);
            Response.End();
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消