Upload Image using WebService C #

2

I would like some help to know how to upload an image using webservice c #, I've tried several forms and several examples but I could not.

I have tried to convert an image to base64 string, I did not succeed. The form for communication between the webservice I used the KSOAP2 library, I also tried JSON, HTTP POST ... Some rotate however do not send photo 640 x 480.

Thank you in advance.

    
asked by anonymous 20.06.2014 / 20:27

1 answer

1

To upload a photo, you can use the WebInvoke annotation, which allows you to create WCF endpoints in REST format. Here's the code snippet to explain:

[WebInvoke(UriTemplate = "UploadPhoto/{fileName}/{description}", Method = "POST")] 
public void UploadPhoto(string fileName, string description, Stream fileContents) 
{ 
    byte[] buffer = new byte[32768]; 
    MemoryStream ms = new MemoryStream(); 
    int bytesRead, totalBytesRead = 0; 
    do 
    { 
        bytesRead = fileContents.Read(buffer, 0, buffer.Length); 
        totalBytesRead += bytesRead; 

        ms.Write(buffer, 0, bytesRead); 
    } while (bytesRead > 0); 

    // Save the photo on database. 
    using (DataAcess data = new DataAcess()) 
    { 
        var photo = new Photo() { Name = fileName, Description = description, Data = ms.ToArray(), DateTime = DateTime.UtcNow }; 
        data.InsertPhoto(photo); 
    } 

    ms.Close(); 
    Console.WriteLine("Uploaded file {0} with {1} bytes", fileName, totalBytesRead); 
} 

This excerpt comes from a complete example with Client and Server for image upload

20.06.2014 / 20:44