Alamofire 5上传编码完成

问题描述 投票:2回答:2

我正在使用Swift 4和Alamofire 5,我上传了两张多张照片,我想打印进度

 AF.upload(
        multipartFormData: { MultipartFormData in

            MultipartFormData.append(firstPic, withName: "first_pic", fileName: "image.jpeg", mimeType: "image/jpeg")
            MultipartFormData.append(secondPic, withName: "second_pic", fileName: "image.jpeg", mimeType: "image/jpeg")

    }, to: urlString, encodingCompletion: { encodingResult in
        switch encodingResult {
        case .Success(let upload, _, _):
            upload.progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
                print(totalBytesRead)
            }
            upload.responseJSON { request, response, result in
                print(result)
            }
        case .Failure(let encodingError):
            print(encodingError)
        }
    })

这就是错误的说法

参数标签'(multipartFormData:,to:,encodingCompletion :)'与任何可用的重载都不匹配

库更新了代码还是什么?

swift alamofire
2个回答
2
投票

Alamofire 5不再需要encodingCompletion!相反,多部分表单编码是作为标准现在异步请求过程的一部分完成的,并且将在Request上返回错误,并且它们在validateresponse*调用期间可用。


3
投票

请根据您的需要进行修改

func upload(image: Data, to url: Alamofire.URLRequestConvertible, params: [String: Any]) {
    AF.upload(multipartFormData: { multiPart in
        for (key, value) in params {
            if let temp = value as? String {
                multiPart.append(temp.data(using: .utf8)!, withName: key)
            }
            if let temp = value as? Int {
                multiPart.append("\(temp)".data(using: .utf8)!, withName: key)
            }
            if let temp = value as? NSArray {
                temp.forEach({ element in
                    let keyObj = key + "[]"
                    if let string = element as? String {
                        multiPart.append(string.data(using: .utf8)!, withName: keyObj)
                    } else
                        if let num = element as? Int {
                            let value = "\(num)"
                            multiPart.append(value.data(using: .utf8)!, withName: keyObj)
                    }
                })
            }
        }
        multiPart.append(image, withName: "file", fileName: "file.png", mimeType: "image/png")
    }, with: url)
        .uploadProgress(queue: .main, closure: { progress in
            //Current upload progress of file 
            print("Upload Progress: \(progress.fractionCompleted)")
        })
        .responseJSON(completionHandler: { data in
            //Do what ever you want to do with response
        })
}

-1
投票
let headers: HTTPHeaders = [
            /* "Authorization": "your_access_token",  in case you need authorization header */
            "Content-type": "multipart/form-data"
        ]


            AF.upload(
                multipartFormData: { multipartFormData in
                    multipartFormData.append(imageOrVideo!.jpegData(compressionQuality: 0.5)!, withName: "upload_data" , fileName: "file.jpeg", mimeType: "image/jpeg")
            },
                to: "http://----/new.php", method: .post , headers: headers)
                .response { resp in
                    print(resp)


            }
© www.soinside.com 2019 - 2024. All rights reserved.