2012-07-31 104 views
4

所以我一直有這個問題一段時間試圖返回一個PDF內聯響應。我搜索了所有通過幫助編碼頁面尋找答案,並找不到有效的東西。我在下面做了一個簡單的例子。但核心與實際生產完全相同。這個問題並不總是會發生的,但是當它發生時它很煩人。這幾乎就像是一些奇怪的計時問題或其他我沒有看到的事情正在發生。 (0x80004005):找不到文件---> System.Web.HttpException(0x80004005):服務器在發送HTTP頭之後無法追加頭文件。服務器無法在HTTP標頭髮送後添加標頭c#pdf inline

有誰知道如何解決這個問題?

[HttpGet] 
    public ActionResult PDF(string id, bool inline = false) 
    { 
     try 
     { 
      MemoryStream PDFStream = GetPdfStream(id); 

      PDFStream.Position = 0; 

      string PDFFile = "Test.pdf"; 


      if (inline) 
      { 
       Response.AppendHeader("Content-Disposition", string.Format("inline; filename={0}", PDFFile)); 
       Response.BufferOutput = true; 
       return File(PDFStream, MediaTypeNames.Application.Pdf); 
      } 
      else 
       return File(PDFStream, MediaTypeNames.Application.Pdf, PDFFile); 
     } 
     catch (Exception Ex) 
     { 
      throw new HttpException(404, "File Not Found", Ex); 
     } 
    } 
+0

請注意,您要創建無效的內容部署值情況下,你的文件名包含空格,某些分隔符,或者非ASCII字符之前,必須清除頁眉。有關詳細信息,請參閱RFC 6266。 – 2016-02-19 14:43:20

回答

2

第一添加新的

if (inline) 
{ 
    Response.ClearHeaders(); 

Response.AppendHeader("Content-Disposition", string.Format("inline; filename={0}", PDFFile)); 
Response.BufferOutput = true; 
return File(PDFStream, MediaTypeNames.Application.Pdf); 
} 
else 
return File(PDFStream, MediaTypeNames.Application.Pdf, PDFFile); 
+0

我想我試過了。但我會再試一次,看看它是如何工作的。儘管如此,我仍然願意接受其他建議。 – kyleb 2012-07-31 17:12:39

+0

你確定我不應該調用Response.Clear()而不是Response.ClearHeaders()嗎? – kyleb 2012-07-31 17:34:44

+0

@kyleb根據MSDN的Response.Clear()擦除所有緩衝的HTML輸出。但是,Clear方法只會擦除響應主體;它不會擦除響應頭。而你的要求是ClearHeaders – HatSoft 2012-07-31 17:42:57

相關問題