Q:

Alamofire - how to have progress and completion closure with multipart upload

I managed to upload file with multipart-form-data Alamofire upload:
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 can't see a way to track upload progress and have completion block called after upload is completed (or failed). Is there a way to do this in Alamofire?
Note: I am aware that uploading with progress is possible, but I'm looking into multipart-form-data specifically.

A:

Here is a way to have completion, failure and progress closures (thanks to my colleague for point me to the solution):
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
        }
})

A:

The accepted answer is great. This one includes parameters, too:
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()
        }
}) 

