没有临时文件OpenXML文件下载 [英] OpenXML file download without temporary file

查看:214
本文介绍了没有临时文件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文件下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆