MemoryStream响应不起作用 [英] MemoryStream response doesnt work
本文介绍了MemoryStream响应不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是在使用asp.net保存为pdf时使用的,但我使用的是win表格
我应该怎么写Response。因为在胜利形式它不起作用。
为什么?我该怎么用?
This you use when saving to pdf using asp.net but i am using win forms
how should i write Response. because in win forms it doesnt work.
why? what should i use?
using (MemoryStream ms = new MemoryStream())
using(Document document = new Document(PageSize.A4, 25, 25, 30, 30))
using(PdfWriter writer = PdfWriter.GetInstance(document, ms))
{
document.Open();
document.Add(new Paragraph("Hello World"));
document.Close();
writer.Close();
ms.Close();
Response.ContentType = "pdf/application";
Response.AddHeader("content-disposition", "attachment;filename=First_PDF_document.pdf");
Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
}
推荐答案
您可以使用除内存之外的流(例如,二进制文件流)和写给它。
这可能是这样的:
You can use a stream other than a memory-one (for example, a binary file stream) and write to it.
This could look like:
using (FileStream fs = File.Open("First_PDF_document.pdf"))
using(Document document = new Document(PageSize.A4, 25, 25, 30, 30))
using(PdfWriter writer = PdfWriter.GetInstance(document, fs))
{
document.Open();
document.Add(new Paragraph("Hello World"));
document.Close();
writer.Close();
fs.Close();
}
这种方式你不构造一个Response对象(相对于web连接),而是一个实际的文件你的文件系统。
希望这会有所帮助。
PS:你可能需要调整使用的内容阻止一点点;我目前没有在我的计算机上安装IDE,因此无法测试该段代码。
This way you do not construct a Response object (relative to web connections), but rather an actual file on your filesystem.
Hope this helps.
PS: you may have to adapt the content of using blocks a little bit; I currently have no IDE installed on my computer, so unable to test for that piece of code.
这篇关于MemoryStream响应不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文