简体   繁体   English

如何使用MemoryStream代替FileStream

[英]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. 我目前将文件写入文件系统,然后使用FileStream将其作为下载内容提供给用户。 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. 但是,如何从内存中的DocumentFormat.OpenXml.Wordprocessing.Document caf_doc获取Stream dl I think I need to use MemoryStream. 我想我需要使用MemoryStream。

The trouble I am having is getting the document into the MemoryStream. 我遇到的麻烦是将文档放入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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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