Streaming In Memory Word Document using OpenXML SDK w/ASP.NET results in “corrupt” document

前端 未结 7 637
北恋
北恋 2021-01-01 15:01

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.

7条回答
  •  醉话见心
    2021-01-01 15:20

    (This is using OpenXML SDK v 2.10.0 and .Net Core v2.2)

    I know this has been answered but I wanted to throw out another option. It is correct that trying to send a stream back in File() like below will result in a corrupt document:

                    MemoryStream updateStream = new MemoryStream();
                    wordDocument.Save();
                    wordDocument.Clone(updateStream);
    
    
                    return File(updateStream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
    

    `

    A super simple alternative/workaround would be to simply convert your stream to a byte[] like below and it will result in a working word docx

                    MemoryStream updateStream = new MemoryStream();
                    wordDocument.Save();
                    wordDocument.Clone(updateStream);
    
    
                    return File(updateStream.ToArray(), "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
    

提交回复
热议问题