Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alamofire 5 upload encodingCompletion

I'm working with Swift 4 and Alamofire 5, I upload two multibart photos and I want to print the progress

 AF.upload(
        multipartFormData: { MultipartFormData in

            MultipartFormData.append(firstPic, withName: "first_pic", fileName: "image.jpeg", mimeType: "image/jpeg")
            MultipartFormData.append(secondPic, withName: "second_pic", fileName: "image.jpeg", mimeType: "image/jpeg")

    }, to: urlString, encodingCompletion: { encodingResult in
        switch encodingResult {
        case .Success(let upload, _, _):
            upload.progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
                print(totalBytesRead)
            }
            upload.responseJSON { request, response, result in
                print(result)
            }
        case .Failure(let encodingError):
            print(encodingError)
        }
    })

and this gets an error saying

Argument labels '(multipartFormData:, to:, encodingCompletion:)' do not match any available overloads

did the library update the code or something??

like image 784
Sophie Bernard Avatar asked Mar 04 '19 14:03

Sophie Bernard


2 Answers

Please modify according to your need

func upload(image: Data, to url: Alamofire.URLRequestConvertible, params: [String: Any]) {
    AF.upload(multipartFormData: { multiPart in
        for (key, value) in params {
            if let temp = value as? String {
                multiPart.append(temp.data(using: .utf8)!, withName: key)
            }
            if let temp = value as? Int {
                multiPart.append("\(temp)".data(using: .utf8)!, withName: key)
            }
            if let temp = value as? NSArray {
                temp.forEach({ element in
                    let keyObj = key + "[]"
                    if let string = element as? String {
                        multiPart.append(string.data(using: .utf8)!, withName: keyObj)
                    } else
                        if let num = element as? Int {
                            let value = "\(num)"
                            multiPart.append(value.data(using: .utf8)!, withName: keyObj)
                    }
                })
            }
        }
        multiPart.append(image, withName: "file", fileName: "file.png", mimeType: "image/png")
    }, with: url)
        .uploadProgress(queue: .main, closure: { progress in
            //Current upload progress of file 
            print("Upload Progress: \(progress.fractionCompleted)")
        })
        .responseJSON(completionHandler: { response in
            //Do what ever you want to do with response
                if let error = response.error {
                    print(error)
                }
                guard let data = response.value else {
                    return 
                }
                print(value)
        })
}
like image 61
Ajay Singh Mehra Avatar answered Sep 26 '22 17:09

Ajay Singh Mehra


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.

like image 30
Jon Shier Avatar answered Sep 24 '22 17:09

Jon Shier