Upload multiple images in swift using Alamofire
Swift 3Just use "[]" with image upload param to make it array of images.
Alamofire.upload(multipartFormData: { multipartFormData in // import image to request for imageData in imagesData { multipartFormData.append(imageData, withName: "\(imageParamName)[]", fileName: "\(Date().timeIntervalSince1970).jpeg", mimeType: "image/jpeg") } for (key, value) in parameters { multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key) } }, to: urlString, encodingCompletion: { encodingResult in switch encodingResult { case .success(let upload, _, _): upload.responseJSON { response in } case .failure(let error): print(error) } })
This one helps me:
private func urlRequestWithComponentsForUploadMultipleImages(urlString:String, parameters:Dictionary<String, String>, imagesData:[Data], imageName: String) -> (URLRequestConvertible , Data) { // create url request to send var mutableURLRequest = URLRequest(url: NSURL(string: urlString)! as URL) mutableURLRequest.httpMethod = Alamofire.HTTPMethod.post.rawValue let boundaryConstant = "myRandomBoundary12345"; let contentType = "multipart/form-data;boundary="+boundaryConstant mutableURLRequest.setValue(contentType, forHTTPHeaderField: "Content-Type") // create upload data to send var uploadData = Data() // add image for data in imagesData { uploadData.append("\r\n--\(boundaryConstant)\r\n".data(using: String.Encoding.utf8)!) uploadData.append("Content-Disposition: form-data; name=\"\(imageName)\"; filename=\"\(Date().timeIntervalSince1970).jpeg\"\r\n".data(using: String.Encoding.utf8)!) uploadData.append("Content-Type: image/jpeg\r\n\r\n".data(using: String.Encoding.utf8)!) uploadData.append(data) } // add parameters for (key, value) in parameters { uploadData.append("\r\n--\(boundaryConstant)\r\n".data(using: String.Encoding.utf8)!) uploadData.append("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n\(value)".data(using: String.Encoding.utf8)!) } uploadData.append("\r\n--\(boundaryConstant)--\r\n".data(using: String.Encoding.utf8)!) print("upload",parameters) return (mutableURLRequest , uploadData)}
here is my solution and it will work 100% no bugs..
Alamofire.upload(multipartFormData: { (multipartFormData : MultipartFormData) in let count = imageToUpload.count for i in 0..<count{ multipartFormData.append(imageToUpload[i], withName: "morephoto[\(i)]", fileName: "photo\(i).jpeg" , mimeType: "image/jpeg") } for (key, value) in parameterrs { multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key) } print(multipartFormData) }, to: url!) { (result) in switch result { case .success(let upload, _ , _): upload.uploadProgress(closure: { (progress) in print("uploding: \(progress.fractionCompleted)") }) upload.responseJSON { response in print(response.result.value!) let resp = response.result.value! as! NSDictionary if resp["status"] as! String == "success"{ print(response.result.value!) let alert = UIAlertController(title: "Alert", message: "Image Upload Successful", preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, handler: nil)) self.present(alert, animated: true, completion: nil) } else{ } } case .failure(let encodingError): print("failed") print(encodingError) } }
in this the imagetoupload array is the array of image data which i have made before.