使用带有 ASP.NET 的 OpenXML SDK 在内存中流式传输 Word 文档会导致“损坏"文档 [英] Streaming In Memory Word Document using OpenXML SDK w/ASP.NET results in "corrupt" document

查看:34
本文介绍了使用带有 ASP.NET 的 OpenXML SDK 在内存中流式传输 Word 文档会导致“损坏"文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将即时创建的 Word 文档流式传输到浏览器.我不断收到来自 Microsoft Word 的消息,指出文档已损坏.

I am unable to stream a word document that I create on the fly down to the browser. I am constantly getting a message from Microsoft Word that the document is corrupt.

当我通过控制台应用程序运行代码并将 ASP.NET 从图片中取出时,文档正确生成,没有任何问题.我相信一切都围绕着把文件写下来.

When I run the code via a Console Application and take ASP.NET out of the picture, the document is generated correctly with no problems. I believe everything centers around writing the file down.

这是我的代码:

using (MemoryStream mem = new MemoryStream())
{
    // Create Document
    using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(mem, WordprocessingDocumentType.Document, true))
    {
        // Add a main document part. 
        MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();

        new Document(new Body()).Save(mainPart);

        Body body = mainPart.Document.Body;
        body.Append(new Paragraph(new Run(new Text("Hello World!"))));

        mainPart.Document.Save();
        // Stream it down to the browser

        // THIS IS PROBABLY THE CRUX OF THE MATTER <---
        Response.AppendHeader("Content-Disposition", "attachment;filename=HelloWorld.docx");
        Response.ContentType = "application/vnd.ms-word.document";
        mem.WriteTo(Response.OutputStream);
        Response.End();
    }
}

查看了很多links - 但没有任何效果.我很多人使用 MemoryStream.WriteTo 并且有些人使用 BinaryWrite - 在这一点上我不确定正确的方法是什么.我也尝试过更长的内容类型,即 application/vnd.openxmlformats-officedocument.wordprocessingml.document 但没有运气.

I have looked at a lot of links – but nothing quite works. I lot of people use MemoryStream.WriteTo and some use BinaryWrite – at this point I'm not sure what the correct way is. Also I've tried the longer content type, i.e. application/vnd.openxmlformats-officedocument.wordprocessingml.document but no luck.

一些屏幕截图 - 即使您尝试恢复,您也会得到相同的部件丢失或无效"

Some screenshots – even if you try to recover you get the same "parts are missing or invalid"

那些偶然发现这个问题的人的解决方案:

WordProcessingDocumentusing 指令中,您必须调用:

Within the using directive of the WordProcessingDocument, you must call:

wordDocument.Save();

还要正确流式传输 MemoryStream,请在外部 using 块中使用它:

Also to correctly stream the MemoryStream, use this in the outer using block:

Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
Response.AppendHeader("Content-Disposition", "attachment;filename=HelloWorld.docx");
mem.Position = 0;
mem.CopyTo(Response.OutputStream);
Response.Flush();
Response.End();

推荐答案

改用CopyToWriteTo有一个bug,导致无法写入全部内容当目标流不支持一次性写入所有内容时的缓冲区.

Use CopyTo instead, there is a bug in WriteTo which makes it fail to write the entire content of the buffer when the target stream does not support writing everything in one go.

这篇关于使用带有 ASP.NET 的 OpenXML SDK 在内存中流式传输 Word 文档会导致“损坏"文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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