Created
September 8, 2017 07:31
-
-
Save mspvirajpatel/0ea94a55066fa36240320000a0776acc to your computer and use it in GitHub Desktop.
Try This Code for Multiple upload Images in Single Request, This code is already working.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For Pass Valid Parameters & number of Images in Array in Image Upload Function | |
var dicImgData : NSMutableDictionary? = NSMutableDictionary() | |
if let img = UIImage(named: "Your Image") { | |
if let data:Data = UIImagePNGRepresentation(img) { | |
var imageData : NSData = data | |
dicImgData! .setObject(imageData, forKey: "data" as NSCopying) | |
dicImgData! .setObject("file", forKey: "name" as NSCopying) | |
dicImgData! .setObject("file.png", forKey: "fileName" as NSCopying) | |
dicImgData! .setObject("image/png", forKey: "type" as NSCopying) | |
let dicParameter = [ | |
"hometown": "yalikavak", | |
"living": "istanbul" | |
] | |
self.uploadImage(url: "Your URL", Parameter: dicParameter, Images: [dicImgData]) | |
} | |
} | |
**Upload Function** | |
func uploadImage (url: String, Parameter param : NSDictionary, Images arrImage: NSArray) -> Void | |
{ | |
var requestURL : String! = url | |
let headers = [ | |
"Authorization": "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==", | |
"Accept": "application/json", | |
] | |
print("---------------------") | |
print("Request URL :- \(requestURL)") | |
print("---------------------") | |
Alamofire.upload(multipartFormData: { (data) in | |
for (key, value) in param { | |
data.append((value as! String).data(using: .utf8)!, withName: key as! String) | |
} | |
for imageInfo in arrImage | |
{ | |
var dicInfo : NSDictionary! = imageInfo as! NSDictionary | |
data.append(dicInfo["data"] as! Data, withName: dicInfo["name"] as! String, fileName: dicInfo["fileName"] as! String, mimeType: dicInfo["type"] as! String) | |
dicInfo = nil | |
} | |
}, to: requestURL, method: .post , headers:nil, encodingCompletion: { (encodeResult) in | |
switch encodeResult { | |
case .success(let upload, _, _): | |
upload.responseJSON(completionHandler: { (response) in | |
switch response.result | |
{ | |
case .success(let responseJSON): | |
guard let dicResponse = responseJSON as? NSDictionary else{ | |
return | |
} | |
print("Response : \((dicResponse))") | |
case .failure(let error): | |
print(error) | |
break | |
} | |
}) | |
case .failure(let error): | |
print(error) | |
break | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment