本文介绍了上传使用模型使用ASP.NET的WebAPI图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的Windows Phone 8的应用程序上传图像使用的WebAPI的SQL Server数据库。我使用的图像一个模型类,它只是由属于图像是针对项目的ID,图片的名称和字节数组保存图像本身。我使用WebClient的对象来访问的WebAPI方法。当我尝试上传的图片,它会抛出异常,如下图所示。有没有人有一个想法,为什么它的错误?另外,我开到图像存储到我的SQL数据库的其他方法。感谢您在看看!

code

 私人MemoryStream的照片流;

...

 私人无效上传()
        {
            尝试
            {
                图片形象=新的图像();
                image.ImagesBytes = photoStream.ToArray();
                image.ImagesID = 3;
                image.ImagesCaption =这是一个测试;                字符串jsonData = JsonConvert.SerializeObject(图片);                Web客户端Web客户端=新的WebClient();
                webClient.Headers [内容类型] =应用/ JSON;
                webClient.Encoding = Encoding.UTF8;                开放的我们的uri =新的URI(HTTP:// MYIP / API /图片/,UriKind.Absolute);
                webClient.UploadStringCompleted + =新UploadStringCompletedEventHandler(webClient_UploadStringCompleted);
                webClient.UploadStringAsync(URI,GET,jsonData);
            }
            抓住
            {
                //显示上传的消息
                tbError.Visibility = Visibility.Visible;
            }
        }

...

 无效webClient_UploadStringCompleted(对象发件人,UploadStringCompletedEventArgs E)
        {
            尝试
            {
                图片形象= JsonConvert.DeserializeObject<图像及GT;(e.Result);
            }
            赶上(异常前)
            {
                //显示上传的消息
                tbError.Visibility = Visibility.Visible;
            }
        }

例外

  System.Net.ProtocolViolationException:操作无效由于对象的当前状态。
   在System.Net.Browser.ClientHttpWebRequest.InternalBeginGetRequestStream(AsyncCallback的回调,对象状态)
   在System.Net.Browser.ClientHttpWebRequest.BeginGetRequestStream(AsyncCallback的回调,对象状态)
   在System.Net.WebClient.UploadBits(WebRequest的要求,流readStream,字节[]缓冲区,字节[]的头,字节]躯,CompletionDelegate completionDelegate,AsyncOperation asyncOp)
   在System.Net.WebClient.UploadDownloadBits(WebRequest的要求,流readStream,流writeStream,字节[]缓冲区,字节[]的头,字节]躯,CompletionDelegate upCompletionDelegate,CompletionDelegate downCompletionDelegate,AsyncOperation asyncOp)
   在System.Net.WebClient.UploadStringAsync(URI地址,字符串的方法,字符串数据,对象userToken)


解决方案

我相信你的HTTP方法不正确。

而不是...

  webClient.UploadStringAsync(URI,GET,jsonData);

尝试...

  webClient.UploadStringAsync(URI,POST,jsonData);

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)
解决方案

I believe your HTTP method is incorrect.

Instead of ...

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

try ...

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

这篇关于上传使用模型使用ASP.NET的WebAPI图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 19:01