4

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.

4
  • Create MemoryStream and "store" the byte[] array into it. Commented Dec 20, 2016 at 14:08
  • 3
    An external web service accepts a file stream? That doesn't seem to be possible. A file stream is on your disc and the web service is accessible through a network. These are two entirely different things. Commented Dec 21, 2016 at 8:31
  • I agree with @Sefe .Roughly speaking, a FileStreamis a wrapper on an handle for a resource (a file on the disk usually). this handle has a meaning only locally, on the computer where you create the FileStream. When you pass this object to an external web server, the underlying handle will not have any meaning. Commented Dec 21, 2016 at 8:55
  • @css How can a web service only except FileStream? How do you access this service? Commented Feb 27, 2019 at 10:51

3 Answers 3

5

Use MemoryStream:

using(var stream = new MemoryStream(byteArray)){ SendStreamToService(stream); } 
Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

see my comment above original post: even if you use a ram disk, I think the FileStream cannot work when you transfer it over a network
@GianPaolo: Maybe OP says web service, but means something else.
0

This is enough. try it.

MemoryStream memStream = new MemoryStream(byteArray);

1 Comment

Why duplicate an answer that already exist?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.