How to Upload image Using Alamofire in Swift
First Install Pod :- "Alamofire"
write this code in you file:-
Alamofire.upload(multipartFormData: { (multipartFormData) in
for (key, value) in parameters {
print("Key \(key) Value \(value)")
multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key as String)
}
if arrFile.count > 0
{
for i in 0..<arrFile.count
{
let dictTemp : NSMutableDictionary = arrFile.object(at: i) as! NSMutableDictionary
let imageData : NSData = dictTemp.value(forKey: "imageData") as! NSData
multipartFormData.append((imageData as? Data)!, withName: “Any name” , fileName: “fileName.jpg” , mimeType: “image/jpeg”)
}
}
},to:str ,method : .post , headers: header) { (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})
upload.responseJSON { response in
print(response.result.value)
callback(response.result.value as? NSDictionary,nil)
}
case .failure(let error):
callback(result as? NSDictionary,error as NSError?)
}
}
Comments
Post a Comment