How to load a Word document from an array of bytes

I have the MS Word file itself, which is saved in an array of bytes. I want to download it as if it were on the file system, but with minimal use of Microsoft.Office.Interop.Word, because it is very slow when it gets the .Open(args[]) part .Open(args[]) .

+4
source share
3 answers

Try it....

  byte[] bte = File.ReadAllBytes("E:\\test.doc"); // Put the Reading file File.WriteAllBytes(@"E:\\test1.doc", bte); // Same contents you will get in byte[] and that will be save here 
+3
source

There is no supported way to do this right now using Interop.Word , as there are no methods for supporting byte arrays.

As a viable workaround, you can use a temporary file as follows:

 // byte[] fileBytes = getFileBytesFromDB(); var tmpFile = Path.GetTempFileName(); File.WriteAllBytes(tmpFile, fileBytes); Application app = new word.Application(); Document doc = app.Documents.Open(filePath); // .. do your stuff here ... doc.Close(); app.Quit(); byte[] newFileBytes = File.ReadAllBytes(tmpFile); File.Delete(tmpFile); 

For more information, read this post .

+2
source

The method public static byte[] ReadAllBytes( string path ) returns all file information to an array of bytes. You don’t need to worry about the stream, as the MSDN documentation says: "Given the path to the file, this method opens the file, reads the contents of the file into an array of bytes and then closes the file."

Go to this link if you want more information.

-one
source

Source: https://habr.com/ru/post/1496536/


All Articles