Alamofire: Uploading MultipartFormData - Progress

Created on 12 Aug 2015  ·  7Comments  ·  Source: Alamofire/Alamofire

Hi,
Is it possible to add
.progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
print(totalBytesRead)
}
to Uploading MultipartFormData ?

question

Most helpful comment

Of course! Here's how you would do it:

Alamofire.upload(
    .POST,
    URLString: "http://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.progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
                print(totalBytesRead)
            }
            upload.responseJSON { request, response, result in
                debugPrint(result)
            }
        case .Failure(let encodingError):
            print(encodingError)
        }
    }
)

All 7 comments

There is a progress() in Request itself, that is the one you are looking for.

Of course! Here's how you would do it:

Alamofire.upload(
    .POST,
    URLString: "http://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.progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
                print(totalBytesRead)
            }
            upload.responseJSON { request, response, result in
                debugPrint(result)
            }
        case .Failure(let encodingError):
            print(encodingError)
        }
    }
)

hi guys,
how can i use this method to upload an image with UIImagePickerViewController?

I'm getting this error while trying this.

XCode 7.1.1, Target OS: iOS 9.1

screenshot at dec 27 00-47-46

I have the same error! Any Progress on that?

Use

upload.responseJSON { response in
                debugPrint(result)
            }

instead of

upload.responseJSON { request, response, result in
                debugPrint(result)
            }

About "Cannot call value of non-function type 'NSProgress'" error: it's fixed by renaming or removing "progress" field of type NSProgress in Request class. Then it is able to choose the right "progress" of function type.

Was this page helpful?
0 / 5 - 0 ratings