ios - How send post data with text parameter and image file? -
i make application allows user upload image , several our server. if post text data code post data server when try post text data image data, server throw exception.
here code:
class func enroll(image: uiimage, date: string, studentid: string, guardianid:string, type:string){ //date,student_id,type,guardian_id,image, type var textbody : string? let imagedata = uiimagepngrepresentation(image) let boundary = nsstring(format: "---------------------------14737809831466499882746641449") let contenttype = nsstring(format: "multipart/form-data; boundary=%@",boundary) let url: nsurl = nsurl(string: "http://api-cc.appsence.net/v1/signs")! let request:nsmutableurlrequest = nsmutableurlrequest(url:url) request.httpmethod = "post" let body : nsmutabledata = nsmutabledata() body.appenddata(nsstring(format: "\r\n--%@\r\n", boundary).datausingencoding(nsutf8stringencoding)!) body.appenddata(nsstring(format:"content-disposition: form-data; name=\"profile_img\"; filename=\"img.jpg\"\\r\n").datausingencoding(nsutf8stringencoding)!) body.appenddata(nsstring(format: "content-type: application/octet-stream\r\n\r\n").datausingencoding(nsutf8stringencoding)!) body.appenddata(imagedata!) body.appenddata(nsstring(format: "\r\n--%@\r\n", boundary).datausingencoding(nsutf8stringencoding)!) //fill text value here textbody = "date=\(date)&student_id=\(studentid)&guardian_id=\(guardianid)&type=\(type)" print("text body : \(textbody!)") let bodytextdata = textbody!.datausingencoding(nsutf8stringencoding); body.appenddata(bodytextdata!) request.addvalue(contenttype string, forhttpheaderfield: "content-type") // if data string use data using encoding request.httpbody = body nsurlsession.sharedsession().datataskwithrequest(request){ (data:nsdata?, response:nsurlresponse?, error:nserror?) -> void in if error == nil{ print("sukses : \(nsstring(data: data!, encoding: nsutf8stringencoding)!)") }else{ print("ini error save \(nsstring(data: data!, encoding: nsutf8stringencoding)!)") } }.resume() }
how can fix issue?
alamofire 3+
// import alamofire func uploadwithalamofire() { let image = uiimage(named: "myphoto")! // define parameters let parameters = [ "hometown": "bodrum", "living": "izmir" ] // begin upload alamofire.upload(.post, "upload_url", // define headers here headers: ["authorization": "auth_token"], multipartformdata: { multipartformdata in // import image request if let imagedata = uiimagejpegrepresentation(image, 1) { multipartformdata.appendbodypart(data: imagedata, name: "file", filename: "myimage.png", mimetype: "image/png") } // import parameters (key, value) in parameters { multipartformdata.appendbodypart(data: value.datausingencoding(nsutf8stringencoding)!, name: key) } }, encodingmemorythreshold: manager.multipartformdataencodingmemorythreshold, encodingcompletion: { encodingresult in switch encodingresult { case .success(let upload, _, _): upload.responsejson { result in debugprint(result) } case .failure(let encodeerror): print(encodeerror) } }) }
Comments
Post a Comment