2011-08-05 86 views
0

當我的用戶單擊鏈接時,我想爲PDF提供標準的打開/保存對話框,而不是在瀏覽器中打開它...使用ffg代碼生成PDF:當pdf在瀏覽器中打開時提供打開/保存選項

Document document = new Document(); 
     MemoryStream stream = new MemoryStream(); 
     try 
     { 
      PdfWriter pdfWriter = PdfWriter.GetInstance(document, stream); 
      pdfWriter.CloseStream = false; 

      document.Open(); 
      document.Add(new Paragraph("Hello World")); 
     } 
     catch (DocumentException de) 
     { 
      Console.Error.WriteLine(de.Message); 
     } 
     catch (IOException ioe) 
     { 
      Console.Error.WriteLine(ioe.Message); 
     } 

     document.Close(); 

     stream.Flush(); 
     stream.Position = 0; 
     return File(stream, "application/pdf"); 
+0

只是一種猜測,但Response.AddHeader(「內容-Disposition「,」attachment; filename = \ 「」+ attachment.AttachmentName()+「\」「);根據這一點,不是提供PDF類型的內容,而是創建PDF附件,並提示用戶保存或查看 – davethecoder

回答

1

嘗試使用File方法的第三個參數返回時,它提供一個名稱的文件:

return File(stream, "application/pdf", "report.pdf"); 
0
Document document = new Document(); 
    FileStream stream = new FileStream("report.pdf", FileMode.Create); 
    try 
    { 
     PdfWriter pdfWriter = PdfWriter.GetInstance(document, stream); 
     pdfWriter.CloseStream = false; 

     document.Open(); 
     document.Add(new Paragraph("Hello World")); 
    } 
    catch (DocumentException de) 
    { 
     Console.Error.WriteLine(de.Message); 
    } 
    catch (IOException ioe) 
    { 
     Console.Error.WriteLine(ioe.Message); 
    } 

    document.Close(); 

    stream.Flush(); 
    stream.Position = 0; 
    return File(stream, "application/pdf"); 
相關問題