2010-10-01 34 views
0

我在開發中有一個ASP.NET MVC2應用程序,並且在生成服務器上呈現.pdf文件時出現問題。爲什麼我的PDF文檔不能在ASP.NET MVC2中渲染/下載?

在我的Visual Studio 2010集成開發服務器上,一切正常,但是在將應用程序發佈到生產服務器之後,它會中斷。它不會拋出任何異常或錯誤,它只是不顯示文件。

這裏是我用於顯示PDF文檔功能:

public static void PrintExt(byte[] FileToShow, String TempFileName, 
                 String Extension) 
{ 
    String ReportPath = Path.GetTempFileName() + '.' + Extension; 

    BinaryWriter bwriter = 
     new BinaryWriter(System.IO.File.Open(ReportPath, FileMode.Create)); 
    bwriter.Write(FileToShow); 
    bwriter.Close(); 

    System.Diagnostics.Process p = new System.Diagnostics.Process(); 
    p.StartInfo.FileName = ReportPath; 
    p.StartInfo.UseShellExecute = true; 
    p.Start(); 
} 

我的生產服務器運行的是Windows Server 2008和IIS 7

+3

這段代碼看起來將顯示在Web服務器上的PDF。 :) – bzlm 2010-10-01 09:16:44

+0

我很慚愧:)。如果人們沒有努力幫助我,我會刪除我的問題:) – Eedoh 2010-10-11 13:46:57

回答

4

您不能期望打開與服務器上的PDF文件瀏覽關聯的默認程序。請嘗試將文件恢復到這將有效打開它的客戶機上的響應流:

public ActionResult ShowPdf() 
{ 
    byte[] fileToShow = FetchPdfFile(); 
    return File(fileToShow, "application/pdf", "report.pdf"); 
} 

現在導航到/somecontroller/showPdf。如果你想在瀏覽器中的PDF打開,而不顯示下載對話框,你可以嘗試添加以下控制器動作返回前:

Response.AddHeader("Content-Disposition", "attachment; filename=report.pdf"); 
0

這裏是我做到了。

public ActionResult PrintPDF(byte[] FileToShow, String TempFileName, String Extension) 
    { 
     String ReportPath = Path.GetTempFileName() + '.' + Extension; 

     BinaryWriter bwriter = new BinaryWriter(System.IO.File.Open(ReportPath, FileMode.Create)); 
     bwriter.Write(FileToShow); 
     bwriter.Close(); 

     return base.File(FileToShow, "application/pdf"); 
    } 

謝謝大家的努力。我使用的解決方案與Darin的解決方案最相似(幾乎相同,但他更漂亮:D),所以我會接受他的解決方案。

投票爲所有你們這些人(包括答案​​和註釋)

致謝

相關問題