views:

98

answers:

2

I have a WCF restful service that I'm trying to upload an image to. I have a very basic metod that accepts a stream as it's only parameter and is defined in the contract as:

[OperationContract]
[WebInvoke(UriTemplate = "ReviewImage", BodyStyle = WebMessageBodyStyle.Bare, Method = "POST")]
ReviewImage UploadImage(Stream data);

I'm actually consuming this service from flash (which is fairly inconsequntial) which selects a file from the file system and uploads it through the service url.

It all works seems to work, adding a breakpoint to the UploadImage method breaks as expected.

If I wanted to save this file back to disk, is it just a case of reading this Stream object into a FileStream object that creates the file somewhere? A bit like the this? When i do actually do this the file can not be opened as an image. I'm sure i'm missing a key piece of knowledge here. Does my stream actually contain just the image bytes or does it contain more than that?

EDIT AFTER ANSWER ACCEPTED:

The problem was that flash encodes image uploads as multipart/form-data which was adding aditional data to the message body. I use the MultipartParser found here to get to the actual image and write to disk.

+1  A: 

As long as the stream was unaltered from the original file, all you need to do is open a new file stream and copy the image stream into it (making sure you're at the start of the stream and making sure you use a reliable string copying algorithm like the one you linked). Its (normally) just as simple as that. It may get complicated if the app sending you the file messes with the stream. But there's only one way to find out if that's the case...

Will
A: 

I would use the Bitmap class in the System.Draming namespace (you need to add a reference to System.Drawing too) personally, that ensure sthe correct encoding.

        Bitmap bmp = new Bitmap(data);
        bmp.Save("Filename", ImageFormat.Jpeg);
Pharabus