Using HttpRequest in Windows Phone -
i have binary stream takes photo stream photochooser
task in windows phone. trying upload picture user chooses onto web server.
how copy photo stream httprequest stream?
so far have this
binarystream bstream = new binarystream(streamfromphotochooser); httprequest request = new httprequest()
i know how set properties httprequest uploads right place. problem actual
uploading of picture.
you can write or copy binary stream request stream getting request stream , writing stream it. here sort of code may find useful web request httpwebrequest , problem capy photo stream in httprequest stream done in getrequeststreamcallback()
private void uploadclick() { httpwebrequest myrequest = (httpwebrequest)webrequest.create("your url of server"); myrequest.method = "post"; myrequest.contenttype = "application/x-www-form-urlencoded"; myrequest.begingetrequeststream(new asynccallback(getrequeststreamcallback), myrequest); } private void getrequeststreamcallback(iasyncresult asynchronousresult) { try { httpwebrequest request = (httpwebrequest)asynchronousresult.asyncstate; system.io.stream poststream = request.endgetrequeststream(asynchronousresult); //your binary stream upload binarystream bstream = new binarystream(streamfromphotochooser); //copy data request stream bstream.copyto(poststream); //or byte[] bytearray = //get bytes of choosen picture // write request stream. poststream.write(bytearray, 0, postdata.length); poststream.close(); //start asynchronous operation response request.begingetresponse(new asynccallback(getresponsecallback), request); } catch (webexception e) { } } private void getresponsecallback(iasyncresult asynchronousresult) { try { httpwebrequest request = (httpwebrequest)asynchronousresult.asyncstate; httpwebresponse response = (httpwebresponse)request.endgetresponse(asynchronousresult); //to read server responce stream streamresponse = response.getresponsestream(); string responsestring = new streamreader(streamresponse).readtoend(); // close stream object streamresponse.close(); // release httpwebresponse response.close(); dispatcher.begininvoke(() => { //update ui here }); } } catch (webexception e) { //handle non success exception here } }
Comments
Post a Comment