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
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);
}
5 Comments
gberg927
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.
Samich
Simply copy the received stream to the file stream and on the second sample.
gberg927
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?
Samich
You need to transfer the file name too. By
Stream or byte[] impossible to identify the file name.Darrel Miller
@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