Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using FileStream without an actual file

I wanted to use the FileStream class to avoid saving any files on the client's computer. But every constructor of FileStream requires a path to a file.

How do I create a FileStream from a stream and not have to save it on the harddrive?

EDIT: I am already using memorystream to store my information. But at some point I need to zip the files into ANOTHER STREAM. The problem is that the zip commands (I've looked in GZipStream - It zips FILES to a STREAM) require me a source filePath(FileStream).

If I can't surpass by creating a FileStream from a stream, is there another way to zip streams?

like image 471
user779444 Avatar asked Mar 31 '12 07:03

user779444


1 Answers

In this scenario you should just use a MemoryStream, and ensure your API just demands Stream rather than FileStream specifically. For example:

using(var ms = new MemoryStream()) {
     YourCode(ms);
}

Alternatively you could keep using FileStream by using a file in the temp area, via Path.GetTempFileName().

like image 176
Marc Gravell Avatar answered Sep 28 '22 04:09

Marc Gravell