Streaming In Memory Word Document using OpenXML SDK w/ASP.NET results in "corrupt" document
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.
As variant for .NET Framework 3.5 and lower. This version of framework haven't method CopyTo
in class Stream
. Therefore, method WriteTo
is replaced by next code:
byte[] arr = documentStream.ToArray();
fileStream.Write(arr, 0, arr.Length);
Example was found by http://blogs.msdn.com/b/mcsuksoldev/archive/2010/04/09/creating-a-new-microsoft-word-document-from-a-template-using-openxml.aspx
I believe your ContentType value is incorrect; that is for Word 97 - 2003 format. Change it to:
application/vnd.openxmlformats-officedocument.wordprocessingml.document
and see if that fixes the problem.