没有临时文件OpenXML文件下载

编程入门 行业动态 更新时间:2024-10-12 16:22:51
本文介绍了没有临时文件OpenXML文件下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有没有提供在一个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 我只发现了一个教程使用一个临时文件,但我认为关于使用 Wordpro​​cessingDocument.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文件下载

本文发布于:2023-07-06 07:41:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047435.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:临时文件   文件   OpenXML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!