Alamofire - how to make progress and complete closure with multi-page loading

I was able to download the Alamofire boot file using multipart-form-data:

Alamofire.upload(.POST, "api.myservice.com", headers: myheaders, multipartFormData: { (multipartFormData:MultipartFormData) -> Void in

    multipartFormData.appendBodyPart(data: json, name: "metadata", mimeType: "application/json")
    multipartFormData.appendBodyPart(data: self.data, name: "document", fileName: "photo.png", mimeType: "image/png")

}, encodingMemoryThreshold: 10 * 1024 * 1024) { (result:Manager.MultipartFormDataEncodingResult) -> Void in
}

but I don’t see a way to track the download process and it has a completion block after the download is completed (or failed). Is there any way to do this in Alamofire?

Note. I know that downloading is possible, but I am viewing the data in multipart-form format.

+4
source share
2 answers

Here's a way to end, refuse, and close the run (thanks to my colleague for pointing this out):

Alamofire.upload(.POST, absPath(), headers: headers(), multipartFormData: { (multipartFormData:MultipartFormData) -> Void in

    multipartFormData.appendBodyPart(data: json, name: "metadata", mimeType: "application/json")
    multipartFormData.appendBodyPart(data: self.data, name: "document", fileName: "photo.png", mimeType: "image/png")

    }, encodingMemoryThreshold: 10 * 1024 * 1024, encodingCompletion: { (encodingResult) -> Void in
        switch encodingResult {
        case .Success(let upload, _, _):
            upload.responseJSON { response in
                // success block
        }
            upload.progress { _, totalBytesRead, totalBytesExpectedToRead in
                let progress = Float(totalBytesRead)/Float(totalBytesExpectedToRead)
                // progress block
            }
        case .Failure(_):
            // failure block
        }
})
+14
source

The accepted answer is great. This includes the parameters:

let image = UIImage(named: "big.jpg")!
let imageData = UIImageJPEGRepresentation(image, 1)

let coupon = textBox?.text ?? "NO coupon"
let uploadUrl = "http://example.com/upload.php"
// define parameters
let parameters : [String: String] = ["one":"some param", "two":"some other param"]

Alamofire.upload(.POST, uploadUrl, headers: nil,
    multipartFormData: { multipartFormData in
        multipartFormData.appendBodyPart(data: imageData!, name: "cannotBeBlank", fileName: "image.zip", mimeType: "image/png")

        // import parameters
        for (key, value) in parameters {
            multipartFormData.appendBodyPart(data: value.data!, name: key)
        }
    }, encodingCompletion: { encodingResult in
        switch encodingResult {
            case .Success(let upload, _, _):
                upload.progress { bytesWritten, totalBytesWritten, totalBytesExpectedToWrite in
                    print("1) \(bytesWritten)    2) \(totalBytesWritten)    3) \(totalBytesExpectedToWrite)")

                    // This closure is NOT called on the main queue for performance
                    // reasons. To update your ui, dispatch to the main queue.
                    dispatch_async(dispatch_get_main_queue()) {
                        print("Total bytes written on main queue: \(totalBytesWritten)")
                    }
                }


                upload.responseData(self.handleResponse)
            case .Failure:
                self.handleError()
        }
}) 
+4
source

Source: https://habr.com/ru/post/1618348/


All Articles