+ Reply to Thread
Results 1 to 2 of 2

Thread: POST image to WCF REST service from aspx page

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    3

    POST image to WCF REST service from aspx page

    I have aspx page where user can upload image. I have to pass the uploaded image to the WCF REST service. How can I do that using POST request?

  2. #2
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Assuming that you are using FileUpload control, first you need to convert image to byte[]. Then you have to add it to the request payload.
    Code:
    		    MemoryStream ms = new MemoryStream();
                        FileUpload1.FileContent.CopyTo(ms);
                        bytearray = ms.ToArray();
                      
                        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(baseAddress);
                        request.Method = "POST";
                        request.ContentType = "application/x-www-form-urlencoded";
                        request.ContentLength = bytearray.Length;
    					
    					Stream serverStream = request.GetRequestStream();
                        serverStream.Write(bytearray, 0, bytearray.Length);
                        serverStream.Close(); ;
    
                        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                        {
                            StreamReader reader = new StreamReader(response.GetResponseStream());
                            string result = reader.ReadToEnd();
                        }

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts