Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 返回损坏文件的PDF_C#_Asp.net_Pdf - Fatal编程技术网

C# 返回损坏文件的PDF

C# 返回损坏文件的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

我使用以下代码将pdf文件发送回用户。这在我的电脑和我们所有的测试电脑上都能正常工作。但是用户抱怨文档已损坏。当我看到用记事本发回的pdf文件时,我可以看到二进制信息后的一些HTML

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