Alamofire 5 Upload Encodingcompletion

Alamofire 5 upload encodingCompletion

Alamofire 5 no longer requires an encodingCompletion! Instead, multipart form encoding is done as part of the standard now-asynchronous request process and will return errors on the Request, and they're available during validate and response* calls.

Access encodingResult when uploading with Alamofire 5

Multipart encoding is fully integrated into the now-asynchronous request pipeline in Alamofire 5. That means there's no separate step to use. However, you can use the MultipartFormData type directly, just like you would in the request closure.

let data = MultipartFormData()
data.append(Data(), withName: "dataName")
try data.encode()

how to upload image (Multipart) using Alamofire 5.0.0-beta.3 (Swift 5)

Please refer Below code.

public class func callsendImageAPI(param:[String: Any],arrImage:[UIImage],imageKey:String,URlName:String,controller:UIViewController, withblock:@escaping (_ response: AnyObject?)->Void){

let headers: HTTPHeaders
headers = ["Content-type": "multipart/form-data",
"Content-Disposition" : "form-data"]

AF.upload(multipartFormData: { (multipartFormData) in

for (key, value) in param {
multipartFormData.append((value as! String).data(using: String.Encoding.utf8)!, withName: key)
}

for img in arrImage {
guard let imgData = img.jpegData(compressionQuality: 1) else { return }
multipartFormData.append(imgData, withName: imageKey, fileName: FuncationManager.getCurrentTimeStamp() + ".jpeg", mimeType: "image/jpeg")
}


},usingThreshold: UInt64.init(),
to: URL.init(string: URlName)!,
method: .post,
headers: headers).response{ response in

if((response.error != nil)){
do{
if let jsonData = response.data{
let parsedData = try JSONSerialization.jsonObject(with: jsonData) as! Dictionary<String, AnyObject>
print(parsedData)

let status = parsedData[Message.Status] as? NSInteger ?? 0

if (status == 1){
if let jsonArray = parsedData["data"] as? [[String: Any]] {
withblock(jsonArray as AnyObject)
}

}else if (status == 2){
print("error message")
}else{
print("error message")
}
}
}catch{
print("error message")
}
}else{
print(response.error!.localizedDescription)
}
}
}

Happy to help you :)

Multipart form data upload with multiple images + parameters (Alamofire 5.2)

try this code, I have not tested in Alamofire(5.2)

let baseUrl = "your URL"
let fullUrl = baseUrl + strUrl
var headers : HTTPHeaders = HTTPHeaders(["Content-type" : "multipart/form-data"])
if let header = header{
headers = header
}

guard let url = try? URLRequest(url: fullUrl, method: .post, headers: headers) else {return}
AF.upload(multipartFormData: { (multipartData) in

for i in 0 ..< arrImage.count{
if let imageData = arrImage[i].pngData(){
let mediaName = "media\(i + 1)"
multipartData.append(imageData, withName:mediaName, fileName: "\(Date().timeIntervalSince1970).jpg", mimeType: "file")
}
}
for (key, value) in parameter {
multipartData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
}
}, to: url).responseJSON(queue: .main, options: .allowFragments) { (response) in
switch response.result{
case .success(let value):
print("Json: \(value)")
case .failure(let error):
print("Error: \(error.localizedDescription)")
}
}.uploadProgress { (progress) in
print("Progress: \(progress.fractionCompleted)")
}

Swift - Alamofire.upload - AF.upload issue with the Upload function

Alamofire 5 has removed the need for the encodingCompletion closure when using multipart form encoding. Instead, you can use the normal response handling as seen in other use cases. For example:

AF.upload(multipartFormData: { data in
// Build your multipart form.
}).responseDecodable(of: SomeType.self) { response in
// Handle response.
}


Related Topics



Leave a reply



Submit