流在内存Word文档使用的OpenXML SDK瓦特/ ASP.NET结果"腐败"文件 [英] Streaming In Memory Word Document using OpenXML SDK w/ASP.NET results in "corrupt" document
问题描述
我不能流,我创建一个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.
当我通过一个控制台应用程序运行code,采取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.
下面是我的code:
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();
}
}
我有看着在大量的<一href="http://stackoverflow.com/questions/2477564/why-are-docx-files-being-corrupted-when-downloading-from-an-asp-net-page">links - 但没有相当的工作。我很多人使用MemoryStream.WriteTo和一些使用的BinaryWrite - 在这一点上我不知道正确的方法是什么。此外,我已经试过了较长的内容类型,即应用程序/ 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"
解决方案为那些谁无意中发现了这个问题:
在使用()。本WordProcessingDocument的指令,则必须调用:
Within the using().. directive of the WordProcessingDocument, you must call:
wordDocument.Save();
wordDocument.Save();
另外要正确流将MemoryStream,使用了基于块外的:
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();
推荐答案
使用 CopyTo从
取而代之的是在一个错误的writeTo
这使得它无法写入缓冲区的全部内容时,目标流不支持写入一切一气呵成。
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.
这篇关于流在内存Word文档使用的OpenXML SDK瓦特/ ASP.NET结果&QUOT;腐败&QUOT;文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!