2

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)

1 Answer 1

1

I believe your HTTP method is incorrect.

Instead of ...

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

try ...

webClient.UploadStringAsync(uri, "POST", jsonData);
Sign up to request clarification or add additional context in comments.

1 Comment

Wow! Thanks @Rick Rainey, great spot! Can't believe I didn't notice that all this time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.