iTextSharp生成PDF:如何将pdf发送到客户端并添加提示?

Arm*_*nce 6 c# pdf extjs prompt itextsharp

我使用iTextSharp生成了一个pdf,当它创建时,它会自动保存在我的代码中提供的位置,而不是在客户端的服务器上,当然也没有告诉用户任何东西.

我需要将它发送给客户端,我需要提示一个对话框询问用户他想要保存他的pdf的位置.

我该怎么办?

这是我的pdf代码:

using (MemoryStream myMemoryStream = new MemoryStream())
{
    Document document = new Document();
    PdfWriter PDFWriter = PdfWriter.GetInstance(document, myMemoryStream);

    document.AddHeader("header1", "HEADER1");


    document.Open();

      //..........

    document.Close();

    byte[] content = myMemoryStream.ToArray();

    // Write out PDF from memory stream.
    using (FileStream fs =      File.Create(HttpContext.Current.Server.MapPath("~\\report.pdf")))
    {
        fs.Write(content, 0, (int)content.Length);
    }
Run Code Online (Sandbox Code Playgroud)

编辑

这是我想要http://examples.extjs.eu/?ex=download的结果示例

感谢您的回复,我修改了我的代码:

HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AppendHeader( "Content-Disposition", "attachment; filename=test.pdf");


using (MemoryStream myMemoryStream = new MemoryStream())
{    
Document document = new Document();    
PdfWriter PDFWriter = PdfWriter.GetInstance(document, myMemoryStream);

document.AddHeader("Content-Disposition", "attachment; filename=wissalReport.pdf");

document.Open();

  //..........

document.Close();


byte[] content = myMemoryStream.ToArray();
HttpContext.Current.Response.Buffer = false;  
HttpContext.Current.Response.Clear();         
HttpContext.Current.Response.ClearContent(); 
HttpContext.Current.Response.ClearHeaders();  
HttpContext.Current.Response.AppendHeader("content-disposition","attachment;filename=" + "my_report.pdf");                
HttpContext.Current.Response.ContentType = "Application/pdf";        

//Write the file content directly to the HTTP content output stream.    
HttpContext.Current.Response.BinaryWrite(content);         
HttpContext.Current.Response.Flush();                
HttpContext.Current.Response.End(); 
Run Code Online (Sandbox Code Playgroud)

但我得到这个错误:

Uncaught Ext.Error: You're trying to decode an invalid JSON String: 
%PDF-1.4 %???? 3 0 obj <</Type/XObject/Subtype/Image/Width 994/Height 185/Length 13339/ColorSpace/DeviceGray/BitsPerComponent 8/Filter/FlateDecode>>stream x???|E?
...........
Run Code Online (Sandbox Code Playgroud)

我绝对肯定我的itextsharp创建pdf是正确的,因为我可以将它保存在服务器上,但那不是我需要做的,当我尝试将其发送到客户端我得到上面的错误

提前致谢

Yet*_*ser 5

如果是Web应用程序,您可能希望将pdf作为二进制文件流式传输给用户,这将打开pdf或提示用户保存文件.

记住pdf生成正在服务器上发生,即使用户提供了它在服务器上没有任何用处的路径.请参阅以下链接 -

在你的情况下,你正在生成文件,因此已经有一个二进制流而不是文件,因此你可以直接使用Response.BinaryWrite而不是Response.WriteFile.

修改样本:

Response.Buffer = false;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
//Set the appropriate ContentType.
Response.ContentType = "Application/pdf";
//Write the file content directly to the HTTP content output stream.
Response.BinaryWrite(content);
Response.Flush();
Response.End();
Run Code Online (Sandbox Code Playgroud)


Arm*_*nce 3

解决了

该错误是由于提交操作试图解释响应而无法解释的,因为它不是已知的格式。

我只是设置 window.location 来下载文件,效果很好。

{
xtype:'button',           
text: 'Generate PDF',           
handler: function () {
     window.location = '/AddData.ashx?action=pdf';                   
}
}
Run Code Online (Sandbox Code Playgroud)

除了设置位置之外,您还可以执行 window.open()。

是否下载或打开文件取决于浏览器设置。