Android to WCF: Streaming multi part image and Json string -
android wcf: streaming multi part image , json string
i want create wcf-restful web service method,in need upload image(multipart-form data) along other information (in json format). web service accessed android , iphone application send image , json information as
i want wcf server side , android code, me
i'm trying upload image , text wcf service. create service save upload images:
rest client code
protected void button2_click(object sender, eventargs e) { if (fileupload1.hasfile) { memorystream mstream = new memorystream(); bitmap bmppostedimage = new bitmap(fileupload1.postedfile.inputstream); bmppostedimage.save(mstream, system.drawing.imaging.imageformat.jpeg); byte[] imagearray = mstream.toarray(); mstream.close(); vehiclechecklisttransaction objattachmentrequestdto = new vehiclechecklisttransaction(); objattachmentrequestdto.vehiclechecklisttransactionid = 0; objattachmentrequestdto.vehiclechecklistid = 2; objattachmentrequestdto.ttid = 10; objattachmentrequestdto.userid = 226; objattachmentrequestdto.usertypeid = 4; objattachmentrequestdto.validtill = "215-05-10"; objattachmentrequestdto.currentdate = "215-07-22"; objattachmentrequestdto.currentstatus = "yes"; objattachmentrequestdto.createdby= 226; objattachmentrequestdto.modifiedby = 226; objattachmentrequestdto.createddate = string.empty; objattachmentrequestdto.modifieddate = string.empty; objattachmentrequestdto.notes = "some text"; objattachmentrequestdto.isactive = true; objattachmentrequestdto.filename = ""; objattachmentrequestdto.filepath = ""; objattachmentrequestdto.verifiedby = 226; var serializer = new datacontractjsonserializer(typeof(vehiclechecklisttransaction)); var ms = new memorystream(); serializer.writeobject(ms, objattachmentrequestdto); ms.position = 0; var reader = new streamreader(ms); string requestbody = reader.readtoend(); var client = new restclient(); client.baseurl = new uri("http://localhost:49188/wds_service.svc/"); var request = new restrequest(method.post) { dateformat = dataformat.json.tostring(), resource = "vehiclechecklisttransaction/add" }; if (requestbody != null) request.addparameter("vehiclechecklisttransaction", requestbody); request.addfile("file1", imagearray, "never.jpg"); var response = client.execute(request); } }
wcf service code
public string uploadphoto(stream request) { //read in our stream string... streamreader reader = new streamreader(request); string jsondata = reader.readtoend(); // ..then convert string single "wscustomer" record. javascriptserializer jss = new javascriptserializer(); checklist checklist = jss.deserialize<checklist>(jsondata); try { filestream targetstream = null; stream sourcestream = checklist.filecontents; string guid = guid.newguid().tostring(); //get photofolder path string photofoldername = "trip\\android"; string filename = guid + ".jpeg"; string uripath = "file:\\c:\\inetpub\\wwwroot\\wds\\media\\" + photofoldername + "\\" + filename; string photopath = new uri(uripath).localpath; using (targetstream = new filestream(photopath, filemode.create, fileaccess.write, fileshare.none)) { //read input stream in 6k chunks //and save output stream const int bufferlen = 65000; byte[] buffer = new byte[bufferlen]; int count = 0; while ((count = sourcestream.read(buffer, 0, bufferlen)) > 0) { targetstream.write(buffer, 0, count); } targetstream.close(); sourcestream.close(); } db.checklists.add(checklist); db.savechanges(); return filename + "_" + checklist.checklistid; } catch (exception ex) { throw new faultexception(ex.message); } }
Comments
Post a Comment