ios - Multipart request with ios10 -
i'm working on multipart request swift, ios10.
saved image server code below. , receive 404 error when open image url.
i can't find what's wrong in code though referred webpage - https://newfivefour.com/swift-form-data-multipart-upload-urlrequest.html
please help.
func connecttoserverwithimage(urlstring: string, param: any, image: uiimage, target: nsobject, action: selector) { let url = url(string: urlstring)! var request = urlrequest(url: url) // set request configuration let boundary = "boundary-\(uuid().uuidstring)" request.setvalue("multipart/form-data; boundary=\(boundary)", forhttpheaderfield: "content-type") request.httpmethod = "post" // set body let body = nsmutabledata() let paramdic = param as! dictionary<string, string> // set param body (key, value) in paramdic { body.append("--\(boundary)\r\n".data(using: .utf8, allowlossyconversion: false)!) body.append("content-disposition: form-data; name=\"\(key)\"\r\n\r\n".data(using: .utf8)!) body.append("\(value)\r\n".data(using: .utf8)!) } // set image body let filename = paramdic["\(paramdic["type"]!)_mst_no"]! + ".jpg" body.append("--\(boundary)\r\n".data(using: .utf8, allowlossyconversion: false)!) body.append("content-disposition: form-data; name=\"file\"; filename=\"\(filename)\"\r\n\r\n".data(using: .utf8, allowlossyconversion: false)!) body.append("content-type: image/jpg\r\n\r\n".data(using: .utf8, allowlossyconversion: false)!) body.append(uiimagejpegrepresentation(image, 1)!) body.append("\r\n".data(using: .utf8, allowlossyconversion: false)!) body.append("--\(boundary)--\r\n".data(using: .utf8, allowlossyconversion: false)!) request.httpbody = body data // url configuration let urlconfig = urlsessionconfiguration.ephemeral urlconfig.timeoutintervalforrequest = 5 urlconfig.timeoutintervalforresource = 5 let session = urlsession(configuration: urlconfig) let datatask = session.datatask(with: request) { (data, response, error) in session.finishtasksandinvalidate() dispatchqueue.main.async { if error == nil { if let httpurlresponse = response as? httpurlresponse { if httpurlresponse.statuscode == 200 { { let jsonobject: dictionary<string, any> = try jsonserialization.jsonobject(with: data!, options: .mutablecontainers) as! dictionary<string, any> if jsonobject.isempty { target.perform(action, with: ["res_code:": "null"]) } } catch let _ {} } } } } } datatask.resume() }
sample code multipart api in ios 10:
func api_multipart() { var url_request = urlrequest(url: url(string: employerregapi())!) url_request.httpshouldhandlecookies = false url_request.timeoutinterval = 60 url_request.httpmethod = "post" let boundary = "-----------sample------------" let form_data = "multipart/form-data; boundary=\(boundary)" url_request.setvalue(form_data, forhttpheaderfield: "content-type") let body = nsmutabledata() /*----------------- company -----------------*/ body.appendstring("--\(boundary)\r\n") body.appendstring("content-disposition: form-data; name=\"\(str_company_name_key)\r\n\r\n") body.appendstring("\(str_company_name_val)\r\n") /*--------------- address id ---------------*/ body.appendstring("--\(boundary)\r\n") body.appendstring("content-disposition: form-data; name=\"\(str_address2_key)\r\n\r\n") body.appendstring("\(str_address_val)\r\n") /*--------------- state code ---------------*/ body.appendstring("--\(boundary)\r\n") body.appendstring("content-disposition: form-data; name=\"\(str_country_key)\r\n\r\n") body.appendstring("\(str_country_val)\r\n") /*------------- profile image --------------*/ if (str_photo_val != nil) { body.appendstring("--\(boundary)\r\n") let mimetype = "image/jpg" let deffilename = generate_image_name_multipart() //yourimagename.jpg let imagedata = uiimagejpegrepresentation(str_photo_val!, 1) body.appendstring("content-disposition: form-data; name=\"\(str_photo_key)\"; filename=\"\(deffilename)\"\r\n") body.appendstring("content-type: \(mimetype)\r\n\r\n") body.append(imagedata!) body.appendstring("\r\n") } body.appendstring("--\(boundary)\r\n") url_request.httpbody = body data let postlength: string = "\(uint(body.length))" url_request.setvalue(postlength, forhttpheaderfield: "content-length") let task = urlsession.shared.datatask(with: url_request, completionhandler: { (data, response, error) in if error != nil{ print("error -> \(string(describing: error))") dispatchqueue.main.async { hideloadinghud(for_view: self.view) } return } { let result = try jsonserialization.jsonobject(with: data!, options: []) as? [string:anyobject] print("result -> \(string(describing: result))") dispatchqueue.main.async { hideloadinghud(for_view: self.view) } } catch { print("error -> \(error)") dispatchqueue.main.async { hideloadinghud(for_view: self.view) } } }) task.resume() } func generate_image_name_multipart() -> string { let min: int = 1 let max: int = 999 let randnum: int = (int(arc4random_uniform(3)) % int((max - min) + min)) print("random number: \(randnum)") let dateformate = dateformatter() dateformate.dateformat = "ddmmyyyyhhmmss" let date: string = dateformate.string(from: date()) let img_name: string = "ios_\(date)_\(randnum).jpg" return img_name }
Comments
Post a Comment