在im导出时,一个pdf正在所需的位置下载,但另一个额外的pdf正在下载文件夹中下载
如何停止下载额外的一个pdf?
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + Projname + ".pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
design.RenderControl(htmlWrite);
string myText = stringWrite.ToString().Replace("&", "&");
StringReader sr = new StringReader(myText.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
string strPath = "D:\\WeeklyReport of " + Projname + ".pdf";
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, new FileStream(strPath, FileMode.Create));
pdfDoc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
pdfDoc.Close();
pdfDoc.Dispose();
答案 0 :(得分:0)
这一行
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, new FileStream(strPath, FileMode.Create));
代码要求PdfWriter
写入服务器文件系统上的文件。
如果您不想要,则必须用其他内容替换new FileStream(strPath, FileMode.Create)
,例如HTTP响应输出流:
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
这有潜在的缺点,
如果这些缺点让您担心,您可能想要实例化MemoryStream
,将PdfWriter
定位到它,构建PDF,从流中检索byte[]
,创建一个内容长度标头,表示该数组的大小,最后将数组写入Response.OutputStream
。