简体   繁体   English

Alamofire 4多部分请求上传进度

[英]Alamofire 4 multipart request upload progress

How should I track progress of my multipart upload request using Alamofire 4? 如何使用Alamofire 4跟踪我的分段上传请求的进度?

My encodingCompletion handler: 我的encodingCompletion处理程序:

encodingCompletion: {
        encodingResult in
        switch encodingResult {
        case .success(let uploadRequest, _, _):
            uploadRequest.uploadProgress {
                p in
                print(p.completedUnitCount, p.totalUnitCount)
            }
            break
        case .failure( _):
            print("Failed to encode upload")
        }
}

The error I get says: 我得到的错误说:

Cannot call value of not-function type 'Progress' 无法调用非函数类型'Progress'的值

Try this: 尝试这个:

Alamofire.upload(
        multipartFormData: { multipartFormData in
            multipartFormData.append(URL(string: "http://example.com/url1")!, withName: "one")
            multipartFormData.append(URL(string: "http://example.com/url2")!, withName: "two")
        },
        to: "http://example.com/to",
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    debugPrint(response)
                }
                upload.uploadProgress { progress in

                    print(progress.fractionCompleted)
                }
            case .failure(let encodingError):
                print(encodingError)
            }
        }
    )

You need to wrap the fractionCompleted , totalUnitCount and completedUnitCount with a cast to Int or Float (depending on what you need). 您需要将fractionCompletedtotalUnitCountcompletedUnitCount包装为Int或Float(取决于您需要的内容)。

It works! 有用!

source: https://github.com/Alamofire/Alamofire/issues/1652#issuecomment-259020449 来源: https//github.com/Alamofire/Alamofire/issues/1652#issuecomment-259020449

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM