How do I get a byte array from HttpInputStream for a docx file?

前端 未结 1 1374
再見小時候
再見小時候 2021-02-20 17:18

I am using the method from the first answer in this post: How to create byte array from HttpPostedFile but it doesn\'t work for .docx files for some reason.

//v         


        
相关标签:
1条回答
  • 2021-02-20 17:58

    Turns out that since I am using the stream already (see the controller method in the question), it is empty when I tried to save it.

    I am not sure why I experienced this with docx and not xlsx since they both have their Streams consumed before the save. My guess is it has something to do with the differences in the Aspose.Cells and Aspose.Words implementations.

    Regardless, however, I set the position on the stream back to 0, and it worked.

    //viewmodel.File is HttpPostedFileBase
    
    viewModel.File.InputStream.Position = 0; //<-----This fixed it!
    
    byte[] fileData;
    using (var binaryReader = new BinaryReader(viewModel.File.InputStream))
    {
        fileData = binaryReader.ReadBytes(viewModel.File.ContentLength);
    }
    
    0 讨论(0)
提交回复
热议问题