ios - Add parameters to image; upload api like "userid" = "50" -
func uploadprofilepicture(data : nsdata,completion: (success : bool) -> void) { pkhud.sharedhud.contentview = pkhudtextview(text: "loading...") pkhud.sharedhud.show() alamofire.upload(.post,globalconstants.kupdatepropic,multipartformdata: { multipartformdata in multipartformdata.appendbodypart(data : data, name: "image", filename: helper.timestamp(), mimetype: "image/png") }, encodingcompletion: { encodingresult in switch encodingresult { case .success(let upload, _, _): upload.responsejson { response in debugprint(response) let status : nsstring = response.result.value?.valueforkey("status") as! string if(status .isequaltostring("1")){ let profile_image : nsstring = response.result.value?.valueforkey("profile_image") as! string defaults.setobject(profile_image, forkey:"imageurl") let fileurl = nsurl(string: profile_image string) myviewstate.profile_image = (fileurl?.lastpathcomponent)! pkhud.sharedhud.hide() completion (success: true) }else{ pkhud.sharedhud.hide() completion(success : false) } } case .failure(let encodingerror): print(encodingerror) pkhud.sharedhud.hide() completion(success : false) } }) }
above code upload image. now, want send other parameters upload , want know how many bytes written.
i have tried use links below haven't been able achieve this. uploading file parameters using alamofire
as @anbu-karthik mentioned, can add additional keys making additional calls multipartformdata
object:
var value: int = 10 let data = nsdata(bytes: &value, length: sizeof(int)) multipartformdata.appendbodypart(data: data, name: "userid")
for computing total encoded size, can query multipartformdata
object it's content size after you're done adding body parts it:
print("total content length: \(multipartformdata.contentlength)")
then you'll need figure out how want store value digging out of closure. should trivial.
Comments
Post a Comment