Hi to all
I have created the WCF Rest service to upload images.Now it is working,
but when i open the uploaded image after converting from stream to image it is
showing error can anyone guide what mistake i am doing.
Operation Contract :
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/PostImage", Method = "POST")]
PublicMessage PostImage(Stream obj);
Service Definition :
public PublicMessage PostImage(Upload obj)
{
byte[] buffer = StreamToByte(obj.File); //Function to convert the stream to byte array
FileStream fs = new FileStream(@"D:\ShopMonkeyApp\Desert.jpg", FileMode.Create, FileAccess.ReadWrite);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(buffer);
bw.Close();
return new PublicMessage { Message = "Recieved the image on server" };
}
public static byte[] StreamToByte(Stream stream)
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
Client Application :
string obj = @"D:\Images\Desert.jpg";
var c = new System.Net.WebClient();
c.OpenWrite(string.Concat("http://localhost:50268/shopmonkey.svc", "/PostImage"), "POST");
c.Headers[HttpRequestHeader.ContentType] = "application/octet-stream";
c.UploadFile("http://localhost:50268/shopmonkey.svc/PostImage"), obj);
Thanks & Regards,
Vijay
vijaymathew
0 Points
3 Posts
Upload Image to WCF Rest Service
Jul 19, 2012 02:20 PM|LINK
Hi to all I have created the WCF Rest service to upload images.Now it is working, but when i open the uploaded image after converting from stream to image it is showing error can anyone guide what mistake i am doing. Operation Contract : [OperationContract] [WebInvoke(BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/PostImage", Method = "POST")] PublicMessage PostImage(Stream obj); Service Definition : public PublicMessage PostImage(Upload obj) { byte[] buffer = StreamToByte(obj.File); //Function to convert the stream to byte array FileStream fs = new FileStream(@"D:\ShopMonkeyApp\Desert.jpg", FileMode.Create, FileAccess.ReadWrite); BinaryWriter bw = new BinaryWriter(fs); bw.Write(buffer); bw.Close(); return new PublicMessage { Message = "Recieved the image on server" }; } public static byte[] StreamToByte(Stream stream) { byte[] buffer = new byte[16 * 1024]; using (MemoryStream ms = new MemoryStream()) { int read; while ((read = stream.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, read); } return ms.ToArray(); } } Client Application : string obj = @"D:\Images\Desert.jpg"; var c = new System.Net.WebClient(); c.OpenWrite(string.Concat("http://localhost:50268/shopmonkey.svc", "/PostImage"), "POST"); c.Headers[HttpRequestHeader.ContentType] = "application/octet-stream"; c.UploadFile("http://localhost:50268/shopmonkey.svc/PostImage"), obj); Thanks & Regards, VijayMudasir.Khan
All-Star
15346 Points
3142 Posts
Re: Upload Image to WCF Rest Service
Jul 19, 2012 02:39 PM|LINK
dont see any error here, please check the size and formats are same and whether its opening at client
vijaymathew
0 Points
3 Posts
Re: Upload Image to WCF Rest Service
Jul 19, 2012 03:03 PM|LINK
thanks for the reply Mudasir it is not opening in windows photo viewer as well as in client system ....anything can do about it ?
vijaymathew
0 Points
3 Posts
Re: Upload Image to WCF Rest Service
Aug 03, 2012 08:13 AM|LINK
Finally I have done using parsing-multipart-form-data by refering the following URL
http://antscode.blogspot.in/2009/11/parsing-multipart-form-data-in-wcf.html
Thanks