0

I am working on creating a WCF rest service. I have a function that will return a byte array of a file created by the service. I was wondering how I can create this file, from the byte array on the client side. This is in C#.

1 Answer 1

1

Simply write all received bytes to the file:

File.WriteAllBytes("filename.bin", receivedBytesArray);

Save stream to file:

Stream receivedStream = // do some staff to receive stream

using (FileStream stream = File.OpenWrite("myfilepath.bin"))
{
    receivedStream.CopyTo(stream);
}
Sign up to request clarification or add additional context in comments.

5 Comments

That does seem to make sense. I guess my next question, and maybe I should ask this in a different post. How to I actually receive the byte array. Right now I am using a WebRequest/WebResponse and the byte array is actually being received as a stream.
Simply copy the received stream to the file stream and on the second sample.
So this creates the file? A question that is brought up is, what If I want the file to be named the same as the file being transferred?
You need to transfer the file name too. By Stream or byte[] impossible to identify the file name.
@gberg927 The Content-Disposition header and its filename parameter should be used to provide the name of the file. See greenbytes.de/tech/webdav/rfc6266.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.