Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.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# 下载PDF而不保存在ASP.net处理程序中_C#_Asp.net_Pdf_Save_Handler - Fatal编程技术网

C# 下载PDF而不保存在ASP.net处理程序中

C# 下载PDF而不保存在ASP.net处理程序中,c#,asp.net,pdf,save,handler,C#,Asp.net,Pdf,Save,Handler,我在asp.net应用程序中有一个处理程序。当出现某种请求时,我希望用户获得下载窗口,在那里我放置PDF流。我不希望PDF保存在内存中。 我有下面的代码,但它无法打开,说pdf文件被破坏,无法打开。 如果你认为我的目标不好,请告诉我实现目标的其他方法。 现在我有了这个代码: 文档文档=新文档(PageSize.A4、80、50、30、65); System.IO.Stream outputStream=new System.IO.FileStream(@“C:\inetpub\wwwroot

我在asp.net应用程序中有一个处理程序。当出现某种请求时,我希望用户获得下载窗口,在那里我放置PDF流。我不希望PDF保存在内存中。 我有下面的代码,但它无法打开,说pdf文件被破坏,无法打开。 如果你认为我的目标不好,请告诉我实现目标的其他方法。

现在我有了这个代码:

文档文档=新文档(PageSize.A4、80、50、30、65); System.IO.Stream outputStream=new System.IO.FileStream(@“C:\inetpub\wwwroot\C\wefwe.pdf”,System.IO.FileMode.OpenOrCreate)

PdfWriter.GetInstance(Doc,outputStream);
Doc.Open();
List-htmlaraylist=iTextSharp.text.html.simpleparser.HTMLWorker.parsetList(新的StringReader(T.text),null);
for(int k=0;k
当我保存pdf时,文件是正确的。。。
但当我得到响应时,文件是正确的。demaged…

您需要通过设置
stream.Position=0来倒带流

另外,
Response.Write(stream)
将只打印
stream.ToString()


相反,您应该调用
stream.CopyTo(Response.OutputStream)

,它应该是
MemoryStream
。我犯了一个只适用于.NET4及更高版本的错误。不过,如果您需要的话,还有很多其他的流复制函数。我认为这个复制对我来说不起作用,除了我使用4.0 net之外,还有一些类似这样的错误:Length='context.Response.OutputStream.Length'引发了类型为'System.NotSupportedException'@levanlevi:堆栈跟踪是什么?@SLaks这是我的错误,我关闭了。现在这不是问题,但我现在也无法获得正确的pdf文件。在复制之前,您需要关闭并倒带流。
 MemoryStream stream = new MemoryStream();
                CreatePFD(T.Text, stream);

                context.Response.Write(stream);


                context.Response.ContentType = "application/pdf";
                context.Response.AddHeader("content-disposition", "attachment; filename=" + "Nots__Saved__FIle__fileName.pdf");
            PdfWriter.GetInstance(Doc, outputStream);

            Doc.Open();

            List<IElement> htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(T.Text), null);
            for (int k = 0; k < htmlarraylist.Count; k++)
            {
                Doc.Add((IElement)htmlarraylist[k]);
            }

            outputStream.CopyTo(context.Response.OutputStream);
            Doc.Close();

            context.Response.ContentType = "application/pdf";
            context.Response.AddHeader("content-disposition", "attachment; filename=" + "Nots__Saved__FIle__fileName.pdf");