CSS
CSS

Reputation: 182

Convert byte[] to file stream without writing to disk

We are getting a input from a web service as a byte[] (which we process internally) and we need to upload to another web service which accepts only a file stream.

How can i to convert byte[] to a file stream without writing to disk in C#?


Edit: This is not duplicate. I am not asking how to convert byte[] to memory stream or file steam. I am asking how to convert byte[] to file stream without writing to disk. Please note that, I need to send the data as file steam to a third party web service, which I do not have access. This web service accepts only as file stream.

So far I have below code:

string fileWritePath = "c:\\temp\\test.docx";
//here fileContent is a byte[]
File.WriteAllBytes(fileWritePath, fileContent);
FileStream fileStream = new FileStream(fileWritePath, FileMode.Open, FileAccess.Read);

I do not want to write the file to local disk and create file stream.

Upvotes: 4

Views: 15137

Answers (3)

user23064773
user23064773

Reputation: 1

This is enough. try it.

MemoryStream memStream = new MemoryStream(byteArray);

Upvotes: 0

Keith
Keith

Reputation: 155602

Use MemoryStream:

using(var stream = new MemoryStream(byteArray)){
   SendStreamToService(stream);
}

Upvotes: 5

Sefe
Sefe

Reputation: 14007

If you are bound to a file stream, you have to use it. If you don't want to go through a physical hard drive you can install a ram disc on your system that maps parts of the memory to a virtual drive and use this drive to map your FileStream to.

Upvotes: 0

Related Questions