C# 返回损坏文件的PDF
我使用以下代码将pdf文件发送回用户。这在我的电脑和我们所有的测试电脑上都能正常工作。但是用户抱怨文档已损坏。当我看到用记事本发回的pdf文件时,我可以看到二进制信息后的一些HTMLC# 返回损坏文件的PDF,c#,asp.net,pdf,C#,Asp.net,Pdf,我使用以下代码将pdf文件发送回用户。这在我的电脑和我们所有的测试电脑上都能正常工作。但是用户抱怨文档已损坏。当我看到用记事本发回的pdf文件时,我可以看到二进制信息后的一些HTML protected void btnGetFile_Click(object sender, EventArgs e) { string title = "DischargeSummary.pdf"; string contentType = "app
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);
}
问题的原因是响应对象在文件末尾附加了文件末尾页面的已解析HTML字节。这可以通过调用Response.Close()来防止 已将文件写入缓冲区
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();
}
工作起来很有魅力。它帮助了我,解决了我的问题。谢谢@johnnunn