windows phone 7 - Upload image using ASP.NET WebAPI using a model -
i trying upload image windows phone 8 app sql server database using webapi. using model class images, consists of id belonging item image for, name of image , byte array hold image itself. use webclient object access webapi method. when try upload image, throws exception shown below. have idea why errors? also, open other methods of storing image sql database. having look!
code
private memorystream photostream;
...
private void upload() { try { images image = new images(); image.imagesbytes = photostream.toarray(); image.imagesid = 3; image.imagescaption = "this 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 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 uploaded message tberror.visibility = visibility.visible; } }
exception
system.net.protocolviolationexception: operation not valid due current state of object. @ system.net.browser.clienthttpwebrequest.internalbegingetrequeststream(asynccallback callback, object state) @ system.net.browser.clienthttpwebrequest.begingetrequeststream(asynccallback callback, object state) @ system.net.webclient.uploadbits(webrequest request, stream readstream, byte[] buffer, byte[] header, byte[] footer, completiondelegate completiondelegate, asyncoperation asyncop) @ system.net.webclient.uploaddownloadbits(webrequest request, stream readstream, stream writestream, byte[] buffer, byte[] header, byte[] footer, completiondelegate upcompletiondelegate, completiondelegate downcompletiondelegate, asyncoperation asyncop) @ system.net.webclient.uploadstringasync(uri address, string method, string data, object usertoken)
i believe http method incorrect.
instead of ...
webclient.uploadstringasync(uri, "get", jsondata);
try ...
webclient.uploadstringasync(uri, "post", jsondata);
Comments
Post a Comment