有没有提供在一个ASP.Net页面下载一个新生成的OpenXML(DOCX)文件而不保存在临时文件夹的一种方式?
Is there a way of providing a download in an ASP.Net Page for a freshly generated OpenXML (docx) file without saving it in a temporary folder?
在 MSDN 我只发现了一个教程使用一个临时文件,但我认为关于使用 WordprocessingDocument.MainDocumentPart.GetStream()并直接写入流出来。
On MSDN I only found a tutorial for using a temp file but I thought about using the WordprocessingDocument.MainDocumentPart.GetStream() and directly writing the stream out.
推荐答案在创建文档使用的MemoryStream 作为后备存储。然后创建和通常关闭该文件,并服务于存储器流的内容到客户端。
When you create the document use a MemoryStream as the backing store. Then create and close the document normally and serve the contents of the memory stream to the client.
using(var stream = new MemoryStream()) { using(var doc = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document, true) { ... } stream.Position = 0; stream.CopyTo(Response.OutputStream); }不要只抢到了 MainDocumentPart 因为,顾名思义,这是文档包的只是其中的一部分,而不是一切。
Do not just grab the MainDocumentPart because, as the name implies, this is just one part of the document package, not everything.
您还需要设置响应头的内容类型和配置。
You'll also need to set response headers for content type and disposition.
更多推荐
没有临时文件OpenXML文件下载
发布评论