Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# iTextSharp生成的PDF:如何将PDF发送到客户端并添加提示?_C#_Pdf_Extjs_Itextsharp_Prompt - Fatal编程技术网

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

C# iTextSharp生成的PDF:如何将PDF发送到客户端并添加提示?,c#,pdf,extjs,itextsharp,prompt,C#,Pdf,Extjs,Itextsharp,Prompt,我已经使用iTextSharp生成了一个pdf,当它被创建时,它会自动保存在服务器上代码中提供的位置,而不是客户端,当然也不会告诉用户任何事情 我需要将其发送到客户端,并需要提示一个对话框,询问用户希望将其pdf保存在何处 请问我怎么做 这是我的pdf代码: using (MemoryStream myMemoryStream = new MemoryStream()) { Document document = new Document(); PdfWriter PDFWrit

我已经使用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);
    }
编辑

这是我想要的结果的一个例子

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

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(); 
但我得到了这个错误:

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�
...........
Uncaught Ext.错误:您试图解码无效的JSON字符串:
%PDF-1.4%���� 30 obj流x���|E�
...........
我绝对肯定我的itextsharp创建pdf是正确的,因为我可以将它保存在服务器上,但这不是我需要做的,当我尝试将它发送到客户端时,我遇到了上面的错误


提前感谢

对于web应用程序,您可能希望将pdf作为二进制文件流传输给用户,这将打开pdf或提示用户保存文件

请记住,pdf生成是在服务器上进行的,即使用户提供的路径在服务器上也没有任何用处。请参阅以下连结-

在您的例子中,您正在生成文件,因此已经有一个二进制流而不是文件,因此您可以直接使用而不是

修改样本:

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

目前,您正在文件服务器上保存文件,因此每次请求都会覆盖相同的pdf。如果同时收到两个PDF请求,可能会导致错误

使用
Response
将PDF(从memorystream)返回给用户,并跳过将PDF写入服务器本地文件的过程

浏览器将询问用户文件应保存在何处。比如:

  Response.ContentType = "Application/pdf";
  myMemoryStream.CopyTo(Response.OutputStream);

另请看,使用
内容处置
可以向用户建议文件名。

您需要向用户浏览器发送内容处置标题。从内存来看,代码是这样的:

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

您不需要使用
MemoryStream
。改为使用
Response.OutputStream
。这就是它的目的。无需使用
Response.BinaryWrite()
或任何其他调用来显式编写文档;使用
Response.OutputStream
时,iTextSharp负责向流中写入数据

下面是一个简单的工作示例:

Response.ContentType = "application/pdf";
Response.AppendHeader(
  "Content-Disposition",
  "attachment; filename=test.pdf"
);
using (Document document = new Document()) {
  PdfWriter.GetInstance(document, Response.OutputStream);
  document.Open();
  document.Add(new Paragraph("This is a paragraph"));
}
给你。(获取保存文件的提示)如果您的代码在
web表单中(按钮单击处理程序),请在
using
语句之后向上面的代码示例添加
Response.End()
,这样web表单的HTML输出就不会附加到PDF文档中。

错误来自尝试解释响应的提交操作,该操作无法解释响应,因为响应的格式未知

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

{
xtype:'button',           
text: 'Generate PDF',           
handler: function () {
     window.location = '/AddData.ashx?action=pdf';                   
}
}
除了设置位置,您还可以执行window.open()


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

适用于WebApp/WinForm/Console程序?在继续生成pdf之前,您始终可以请求一个位置。这是一个web应用程序。如何操作?(抱歉,我是c#和iTextSharp的新手)很好,我不需要它在服务器上,我需要用户能够在其端进行保存..将查看此信息并让您知道,我没有使用Response.WriteFile!您在示例中引用的内容与我的代码字节[]content=myMemoryStream.ToArray()中的内容相同?是-内容与代码中的内容相同。您需要使用
Response.BinaryWrite(content)
。它将对pdf Writer创建的pdf进行流式处理。确保
内容
正确(尝试将其保存到二进制文件中),并尝试在
响应之前添加
Response.Flush()
Response.End()
这是我以前使用过的内容,但我遇到了一个问题,pdf未创建,没有提示,但出现错误:意外标记%。在fiddler中,我可以看到我的pdf(我想是这样)中有奇怪的字符:%pdf-1.4%���� 30 obj流x��|E���r=�J Z M���XP_ugdaq���thx,试过了,但没有提示,也没有创建我的pdf。或者至少我不知道它在哪里。你确定这是一个web应用吗?更仔细地阅读你的代码你似乎是在把pdf保存在服务器上而不是发送给客户端。那是因为我不知道怎么做:((…但这正是我想做的..将pdf发送给客户端我知道,iTextSharp将关闭memorystream。使用
Response.BinaryWrite(content)
从Yet的答案中,添加一个
响应。Clear()
。这是我以前尝试过的,但我遇到了一个问题,pdf没有创建,没有提示,但我得到了错误:意外标记%。在fiddler中,我可以看到我的pdf(我想是这样)的奇怪字符:%pdf-1.4%���� 30 obj流x��|E���r=�J Z M���XP_ugdaq���可能您的浏览器缓存了内容类型。请尝试按F5,或使用其他浏览器测试。thx..尝试时,我遇到问题,pdf没有创建,没有提示,但我得到错误:意外标记%。在fiddler中,我可以看到我的pdf(我想是这样)的奇怪字符:%pdf-1.4%���� 30 obj流x��|E���r=�J Z M���XP_ugdaq���“意外令牌%”听起来像是JavaScript错误-当你说你正在使用
fiddler
时更是如此。你是如何尝试将PDF发送到浏览器的?这是Ajax应用程序吗?这是extjs应用程序。我如何将PDF发送到浏览器?我不确定我是否理解..我的代码在我的问题中..生成PDF的代码在问题中,但看起来像是你的JavaScript代码是问题。从未使用过extjs,也许你需要这样做?我已经这样做了,我从我的(exstjs).js客户端发送参数,并调用函数来创建我的pdf,它是a.cs。怎么会有问题?我已经读过了