ios - send multipart request on server by URLSession -


we facing issue in sending image , video on server . using url session request . have used lot of code not almofire .i have checked on server end shows content nil @ server side .

below method using creating body data parameters.

static func createbodywithparameters(parameters: [string: anyobject]?, filepathkey: string?, files : array<dictionary<string, anyobject>>, boundary: string) -> nsdata {      let body : nsmutabledata = nsmutabledata();      if parameters != nil {         (key, value) in parameters! {             body.append(("--\(boundary)\r\ncontent-disposition: form-data; name=\"\(key)\"\r\n\r\n \(value)\r\n" nsstring).data(using: string.encoding.utf8.rawvalue)!)         }     }      file in files     {          let filename : string = "postedfile"//file["filename"] as! string         let filedata : nsdata = file["postedfile"] as! nsdata           body.append(("--\(boundary)\r\ncontent-disposition: form-data; name=\"\(filename)\"; filename=\"\(filename)\"\r\n\r\n" nsstring).data(using: string.encoding.utf8.rawvalue)!)         body.append(filedata data)         body.append(("\r\n" nsstring).data(using: string.encoding.utf8.rawvalue)!)     }     body.append(("--\(boundary)--\r\n" nsstring).data(using: string.encoding.utf8.rawvalue)!)      //        dta = nsstring(data: body data, encoding: string.encoding.utf8.rawvalue)     //        print("final string is: \(string(describing: dta))")     return body } 

below method using posting multipart data on server side

static func uploadvideo(servicename:string , videodata : nsdata, parameter : nsdictionary,controller : anyobject , completeblock:@escaping (_ status : bool, _ data : anyobject?, _ error : nserror?)->()){          if reachabilityforinternet.isconnectedtonetwork() == false {             cierror("no internet connection")             return         }          let boundary = uidevice.current.identifierforvendor!.uuidstring         let dictimage = nsmutabledictionary()         //        dictimage.setobject("file_data", forkey: "filename" nscopying)         dictimage.setobject(videodata , forkey: "postedfile" nscopying)          let arrimagedata = nsarray(object: dictimage)          let session = urlsession.shared                let request = nsmutableurlrequest(url: nsurl(string: "http://192.168.0.54:1000/home/" + "\(servicename)")! url)          request.addvalue("8bit", forhttpheaderfield: "content-transfer-encoding")         request.addvalue("multipart/form-data; boundary=\(boundary)", forhttpheaderfield: "content-type")          let bodydata = createbodywithparameters(parameters: parameter as? [string : anyobject], filepathkey: "filename", files: arrimagedata as! array<dictionary<string, anyobject>>, boundary: boundary)              request.httpmethod = "post"         request.timeoutinterval = 60          var error: nserror?         request.httpbody = bodydata data          if let error = error {             print("\(error.localizeddescription)")         }          var err : nserror?           let datatask = session.datatask(with: request urlrequest) { data, response, error in              print(error?.localizeddescription)              print(string(data: data!, encoding: .utf8))             print(response)               // handle response             let error: autoreleasingunsafemutablepointer<nserror?>? = nil             if data != nil             {                 var jsonresult: nsdictionary!                 jsonresult = nil                  do{                     jsonresult = try jsonserialization.jsonobject(with: data!, options:jsonserialization.readingoptions.mutablecontainers) as? nsdictionary                     print(jsonresult)                 } catch _ {                 }                 if jsonresult == nil{                     if let httpresponse = response as? httpurlresponse {                         let responsecode: int = httpresponse.statuscode                         if responsecode == 200                         {                          }                          else                         {                          }                      }                  }                 if (error != nil)                 {                     print(error ?? "error")                 } else {                  }              }             else             {              }         }          datatask.resume()     } 


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -