I am unable to upload my image to server using multipart data in swift3? -


i unable upload image on server. using multpart form data.i selecting image imagepicker controller.everything set imagerpicker delegate each , .after uploading image not shoeing error image not uploading on server.and when select image using imagepicker shows error creating image format unknown type error app not crashes

this code using in swift3 sending image. here code using.

viewcontroller.swift //i not writnig full code following action of button calling function

    button action{     // image     let image = userimage.image     let unwrappedimage = image!     let data = uiimagejpegrepresentation(unwrappedimage, 1.0)     let params = ["user_dob":dob, "user_gender":"1", "user_name":   username, "user_status":userstatus] nsdictionary     self.uploadrequest(image:data,parameters:params)      } 

my function uploading image , data

    func uploadrequest(image:nsdata,parameters:nsdictionary){     let url = nsurl(string: "myurl")     let request = nsmutableurlrequest(url: url! url)     request.httpmethod = "post"     let boundary = generateboundarystring()     let secretkey = defaults.string(forkey: "secretkey")      request.setvalue(secretkey, forhttpheaderfield: "auth_key")  request.setvalue("multipart/form-data; boundary=\(boundary)",forhttpheaderfield: "content-type")      let body = nsmutabledata()      body.append("--\(boundary)\r\n".data(using: .utf8)!)      body.append("content-disposition:form-data; name=\"test\"\r\n\r\n".data(using: string.encoding.utf8)!)      body.append("hi\r\n".data(using: .utf8)!)      body.append("--\(boundary)\r\n".data(using: .utf8)!)      //body.append("content-disposition:form-data; name=\"file\"; filename=\"\(fname)\"\r\n".data(using: .utf8)!)      //body.append("content-type: \(mimetype)\r\n\r\n".data(using: .utf8)!)      body.append(image data)      body.append("\r\n".data(using: string.encoding.utf8)!)      for(key,value) in parameters{     body.append("--\(boundary)\r\n".data(using: .utf8)!)     body.append("content-disposition:form-data;      name=\"file\";filename=\"\(key)\"\r\n\r\n\(value)".data(using: .utf8)!)       }     body.append("--\(boundary)--\r\n".data(using: .utf8)!)        let session = urlsession.shared      let task = session.datatask(with: request urlrequest) {          (         data, response, error) in          guard let _:nsdata = data nsdata?, let _:urlresponse = response  , error == nil else         {             print("error")              return     }let datastring =nsstring(data:data!,encoding:string.encoding.utf8.rawvalue)          print(datastring)      }      task.resume()   } 


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -