Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 765
CSS Avatar asked Sep 05 '25 03:09

CSS


1 Answers

Use MemoryStream:

using(var stream = new MemoryStream(byteArray)){
   SendStreamToService(stream);
}
like image 89
Keith Avatar answered Sep 07 '25 23:09

Keith