PDF returning a corrupt file
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();
}
精彩评论