问题:
虽然您的数据已写入MemoryStream,但内部“流指针”或游标(在老式术语中,将其视为磁带头)位于您已写入数据的末尾写:
document.Save()之前:
stream = [_________________________...]
ptr = ^
拨打document.Save()后:
stream = [<xml><p>my word document</p><p>the end</p></xml>_________________________...]
ptr = ^
当您调用Controller.File( Stream, String ) 时,它将继续从当前ptr 位置继续读取,因此只读取空白数据:
stream = [<xml><p>my word document</p><p>the end</p></xml>from_this_point__________...]
ptr = ^
(实际上它根本不会读取任何内容,因为MemoryStream 特别不允许读取超出其内部长度限制,默认情况下是到目前为止写入它的数据量)
如果您将ptr 重置为流的开头,则读取流时返回的数据将从写入数据的开头开始:
stream = [<xml><p>my word document</p><p>the end</p></xml>_________________________...]
ptr = ^
解决方案:
在从流中读取数据之前,您需要将 MemoryStream 重置为位置 0:
using Xceed.Words.NET;
// ...
MemoryStream stream = new MemoryStream();
DocX document = DocX.Create( stream );
Paragraph p = document.InsertParagraph();
p.Append("Hello World");
document.Save();
stream.Seek( 0, SeekOrigin.Begin ); // or `Position = 0`.
return File( stream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "DOCHK.docx" );