简体   繁体   中英

How to use MemoryStream Instead of FileStream

I currently write a file to the file system and then use FileStream to offer it up as a download to a user. However, there is no need for this extra step of Document In Memory to File System to Stream. I should just go from Document In Memory to Stream. I am having a heck of a time, though figuring out how to do this.

Right now I am using this:

Stream dl = new FileStream(filepath, FileMode.Open, FileAccess.Read);

But how to do I get Stream dl from the DocumentFormat.OpenXml.Wordprocessing.Document caf_doc that I have in memory. I think I need to use MemoryStream.

The trouble I am having is getting the document into the MemoryStream.

Any help is appreciated.

Create a memory stream and save the document to it. Then reset the position of the memory stream, and you can read from it:

using (MemoryStream m = new MemoryStream()) {
  caf_doc.Save(m);
  m.Position = 0;
  // here you can read from the stream m
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM