OpenXML file download without temporary file

醉酒当歌 提交于 2019-11-30 14:33:59

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);
}

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.

Stream.CopyTo() in .NET 4.0 might help you out here.

WordprocessingDocument.MainDocumentPart.GetStream().CopyTo(Response.OutputStream);

You'll still need to set the headers for MIME type, content-disposition and so on.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!