2009-12-07 109 views
2

我正在使用以下代碼將一個pdf文件發回給用戶。這在我的電腦和我們所有的測試電腦上都能正常工作。但用戶抱怨文檔已損壞。當我看到在記事本中發回的pdf文件時,我可以在二進制信息之後看到一些HTML。PDF返回損壞的文件

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); 
     } 

回答

5

問題是原因由響應對象追加到文件的末尾在文件的結尾字節解析的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(); 
     } 
+0

工程像魅力。它幫助我解決了我的問題。謝謝@John Nunn – Mehmood 2016-03-21 17:10:04