Alamofire: How to cancel MultipartData request?

Created on 27 May 2016  路  5Comments  路  Source: Alamofire/Alamofire

I am not able to cancel below multipart data upload request when internet connection not available. Any suggestions?

Alamofire.upload(
    .POST,
    "https://httpbin.org/post",
    multipartFormData: { multipartFormData in
        multipartFormData.appendBodyPart(fileURL: unicornImageURL, name: "unicorn")
        multipartFormData.appendBodyPart(fileURL: rainbowImageURL, name: "rainbow")
    },
    encodingCompletion: { encodingResult in
        switch encodingResult {
        case .Success(let upload, _, _):
            upload.responseJSON { response in
                debugPrint(response)
            }
        case .Failure(let encodingError):
            print(encodingError)
        }
    }
)
support

Most helpful comment

Hi @lalitkumarchoudhary,

You should store the upload reference as a property that you can call cancel on.

class NetworkManager {
    private var uploadRequest: Request?

    func startUpload() {
        Alamofire.upload(
            .POST,
            "https://httpbin.org/post",
            multipartFormData: { multipartFormData in
                multipartFormData.appendBodyPart(fileURL: unicornImageURL, name: "unicorn")
                multipartFormData.appendBodyPart(fileURL: rainbowImageURL, name: "rainbow")
            },
            encodingCompletion: { encodingResult in
                switch encodingResult {
                case .Success(let upload, _, _):
                    uploadRequest = upload

                    upload.responseJSON { response in
                        debugPrint(response)
                        uploadRequest = nil
                    }
                case .Failure(let encodingError):
                    print(encodingError)
                }
            }
        )
    }

    func cancelUpload() {
        uploadRequest?.cancel()
        uploadRequest = nil
    }
}

Hopefully that helps. In the future, I'd encourage you to ask this type of question on Stack Overflow. We use our GitHub project for bug reports and feature requests.

Cheers. 馃嵒


From our Contribution Guidelines

Asking Questions

We don't use GitHub as a support forum. For any usage questions that are not specific to the project itself, please ask on Stack Overflow instead. By doing so, you'll be more likely to quickly solve your problem, and you'll allow anyone else with the same question to find the answer. This also allows maintainers to focus on improving the project for others.

All 5 comments

Hi @lalitkumarchoudhary,

You should store the upload reference as a property that you can call cancel on.

class NetworkManager {
    private var uploadRequest: Request?

    func startUpload() {
        Alamofire.upload(
            .POST,
            "https://httpbin.org/post",
            multipartFormData: { multipartFormData in
                multipartFormData.appendBodyPart(fileURL: unicornImageURL, name: "unicorn")
                multipartFormData.appendBodyPart(fileURL: rainbowImageURL, name: "rainbow")
            },
            encodingCompletion: { encodingResult in
                switch encodingResult {
                case .Success(let upload, _, _):
                    uploadRequest = upload

                    upload.responseJSON { response in
                        debugPrint(response)
                        uploadRequest = nil
                    }
                case .Failure(let encodingError):
                    print(encodingError)
                }
            }
        )
    }

    func cancelUpload() {
        uploadRequest?.cancel()
        uploadRequest = nil
    }
}

Hopefully that helps. In the future, I'd encourage you to ask this type of question on Stack Overflow. We use our GitHub project for bug reports and feature requests.

Cheers. 馃嵒


From our Contribution Guidelines

Asking Questions

We don't use GitHub as a support forum. For any usage questions that are not specific to the project itself, please ask on Stack Overflow instead. By doing so, you'll be more likely to quickly solve your problem, and you'll allow anyone else with the same question to find the answer. This also allows maintainers to focus on improving the project for others.

@cnoon I've followed the steps you placed above, but my app crashed when the upload request cancel, can you help on SO?

That's awesome @cnoon thanks a lot for the code!

Hello Help me How to Store Multiple Upload Requests and delete based on selected request array in Alamofire i've used swift 2.3 and Alamofire '3.4'

how to upload more than one image data with different image parameters ??

Was this page helpful?
0 / 5 - 0 ratings