I am working on a wcf rest api. I got issue to upload images from Client side to API. From Client side I do POST method to pass Streaming, and In Service, trying to fetch data through Message, StreamReader and some other ways also tried.
I checked in debugging mode, stream has length of 15000 in Client Side, and when I recieve it in service, it is more than 15000, it is 21000.
I am not getting any idea why some extra values are added in Stream.
Please some one give idea to solve this issue.
Thanks
VD
Don't forget to mark this post as "Answer", if it helped you.
vdtrip
Member
70 Points
77 Posts
POST stream data in REST API-WCf
Jan 05, 2013 01:34 AM|LINK
Hi,
I am working on a wcf rest api. I got issue to upload images from Client side to API. From Client side I do POST method to pass Streaming, and In Service, trying to fetch data through Message, StreamReader and some other ways also tried.
I checked in debugging mode, stream has length of 15000 in Client Side, and when I recieve it in service, it is more than 15000, it is 21000.
I am not getting any idea why some extra values are added in Stream.
Please some one give idea to solve this issue.
Thanks
VD
kushalrdalal
Contributor
7130 Points
1273 Posts
Re: POST stream data in REST API-WCf
Jan 07, 2013 12:57 PM|LINK
Check this article-
http://code.msdn.microsoft.com/windowsdesktop/Upload-files-using-a-REST-13f16af2
Check this in your config file-
andand make it to maximum for the testing purpost.
My Blog
LinkedIn Profile
vdtrip
Member
70 Points
77 Posts
Re: POST stream data in REST API-WCf
Jan 09, 2013 06:40 PM|LINK
Finally I got the solution.
public DealerImage PostImage(Stream sm) { System.Drawing.Bitmap imag = new System.Drawing.Bitmap(sm); byte[] imagedata = ImageToByte(imag); //Save byte code to database.. } public static byte[] ImageToByte(System.Drawing.Image img) { ImageConverter converter = new ImageConverter(); return (byte[])converter.ConvertTo(img, typeof(byte[])); } Client Code to POST image....... public void PostData() { try { byte[] fileToSend = null; string name = ""; if (FileUpload1.HasFile) { name = FileUpload1.FileName; Stream stream = FileUpload1.FileContent; stream.Seek(0, SeekOrigin.Begin); fileToSend = new byte[stream.Length]; int count = 0; while (count < stream.Length) { fileToSend[count++] = Convert.ToByte(stream.ReadByte()); } } HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("URL here"); req.Method = "POST"; req.ContentType = "application/octet-stream"; req.ContentLength = fileToSend.Length; Stream reqStream = req.GetRequestStream(); reqStream.Write(fileToSend, 0, fileToSend.Length); reqStream.Close(); HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); StreamReader reader = new StreamReader(resp.GetResponseStream()); string result = reader.ReadToEnd(); } catch (Exception ex) { throw ex; } }