Multipart Request
Sometimes we need to upload the multiple images to a server in a single request, for this Alamofire 3.0 provide the upload facility where we can upload ‘n’ number of images in the single request.
For this we have to follow some steps;
1: First we have to fetch all images in a single array and store all UIImage object in the array.
Ex:
var imageDataArray:NSMutableArray = []
2: Now collect all the data which you have to send like params, headers or any data
Note: don’t access the data which is on the main thread, when you are uploading the images to server because upload works on background thread only.
Ex: like fetching the value from UItextfield etc;
3: define header in ViewdidLoad()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
var headers: HTTPHeaders! override func viewDidLoad() { headers = [:] if defaults.object(forKey: "authKey") == nil{ headers = [ "apiKey": API_USER_NAME, "apiPassword": API_KEY, "authKey":"" ] }else{ headers = [ "apiKey": API_USER_NAME, "apiPassword": API_KEY, "authKey":defaults.object(forKey: "authKey") as! String ] } } |
4: Now its time to define the method :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
func uplaodImages(){ Alamofire.upload(multipartFormData: { multipartFormData in let customerId = self.defaults.object(forKey:"customerId"); var params = [String:AnyObject](); params["customerId"] = customerId as AnyObject params["customerEmail"] = self.customerEmail as AnyObject params["customerName"] = self.customerName as AnyObject params["latitude"] = self.latitude as AnyObject params["longitude"] = self.longitude as AnyObject params["description"] = self.descriptionData as AnyObject for (key, value) in params { if let data = value.data(using: String.Encoding.utf8.rawValue) { multipartFormData.append(data, withName: key) } } for i in 0..<self.imageDataArray.count{ if self.imageFlagArray[i] as! Bool == true{ let imageData1 = UIImageJPEGRepresentation(self.imageDataArray[i] as! UIImage, 0.5)! multipartFormData.append(imageData1, withName: "image"+String(format:"%d",i), fileName: "image.jpg", mimeType: "image/jpeg") print("success"); } } }, to: self.UPLOAD_URL,method:HTTPMethod.post, headers:self.headers, encodingCompletion: { encodingResult in switch encodingResult { case .success(let upload, _, _): upload .validate() .responseJSON { response in switch response.result { case .success(let value): print("responseObject: \(value)") case .failure(let responseError): print("responseError: \(responseError)") } } case .failure(let encodingError): print("encodingError: \(encodingError)") } }) } |
Note: a: imageFlagArray -> is bool array to check the array contains UIImage data or not.
b: self.imageDataArray[i] -> it contains UIImage object of Images.
c: self.UPLOAD_URL – > it is base URL where we have to upload.
d: self.headers -> it is header which I have defined in viewdidload method;
Now check the proper params and header field, upload URL then try it.