Ayman H
Ayman H

Reputation: 185

Upload image using ASP.NET WebAPI using a model

I am trying to upload an image from my windows phone 8 app to the SQL server database using a WebAPI. I am using a model class for the Images, which simply consists of an ID belonging to the item the image is for, the name of the image and a byte array to hold the image itself. I use a WebClient object to access the WebAPI method. When I try to upload the image, it throws the exception as shown below. Does anyone have an idea why it errors? Also, I am open to other methods of storing the image to my SQL database. Thanks for having a look!

Code

private MemoryStream photoStream;

...

private void upload()
        {
            try
            {
                Images image = new Images();
                image.ImagesBytes = photoStream.ToArray();
                image.ImagesID = 3;
                image.ImagesCaption = "this is a test";

                string jsonData = JsonConvert.SerializeObject(image);

                WebClient webClient = new WebClient();
                webClient.Headers["Content-type"] = "application/json";
                webClient.Encoding = Encoding.UTF8;

                Uri uri = new Uri("http://myIP/api/Images/", UriKind.Absolute);
                webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(webClient_UploadStringCompleted);
                webClient.UploadStringAsync(uri, "GET", jsonData);
            }
            catch
            {
                // Display the Uploaded message
                tbError.Visibility = Visibility.Visible;
            }
        }

....

        void webClient_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            try
            {
                Images image = JsonConvert.DeserializeObject<Images>(e.Result);
            }
            catch (Exception ex)
            {
                // Display the Uploaded message
                tbError.Visibility = Visibility.Visible;
            }
        }

Exception

System.Net.ProtocolViolationException: Operation is not valid due to the current state of the object.
   at System.Net.Browser.ClientHttpWebRequest.InternalBeginGetRequestStream(AsyncCallback callback, Object state)
   at System.Net.Browser.ClientHttpWebRequest.BeginGetRequestStream(AsyncCallback callback, Object state)
   at System.Net.WebClient.UploadBits(WebRequest request, Stream readStream, Byte[] buffer, Byte[] header, Byte[] footer, CompletionDelegate completionDelegate, AsyncOperation asyncOp)
   at System.Net.WebClient.UploadDownloadBits(WebRequest request, Stream readStream, Stream writeStream, Byte[] buffer, Byte[] header, Byte[] footer, CompletionDelegate upCompletionDelegate, CompletionDelegate downCompletionDelegate, AsyncOperation asyncOp)
   at System.Net.WebClient.UploadStringAsync(Uri address, String method, String data, Object userToken)

Upvotes: 2

Views: 2657

Answers (1)

Rick Rainey
Rick Rainey

Reputation: 11246

I believe your HTTP method is incorrect.

Instead of ...

webClient.UploadStringAsync(uri, "GET", jsonData);

try ...

webClient.UploadStringAsync(uri, "POST", jsonData);

Upvotes: 1

Related Questions