swift - Alamofire 5 上传编码完成
问题描述
我正在使用 Swift 4 和 Alamofire 5,我上传了两张 multibart 照片,我想打印进度
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:)' 不匹配任何可用的重载
图书馆更新了代码还是什么?
解决方案
请根据您的需要进行修改
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: { response in
//Do what ever you want to do with response
if let error = response.error {
print(error)
}
guard let data = response.value else {
return
}
print(value)
})
}
推荐阅读
- opencv - 如何清除opencv CV_CAP_PROP_BUFFERSIZE
- python - Python Flask 日志记录问题
- typescript - 如何抛出对象交集类型的语法错误?
- vue.js - 使用 Nuxt 对 VueJS 应用程序的 Jest 测试覆盖率
- python - 熊猫和列表理解的速度问题
- php - 发送数组以进行删除时,允许的内存大小已用尽
- xml - 与使用 su 连接多个 XML 标记相关
- java - Codename One & SQLite 连续测试 & 接收 java.lang.UnsatisfiedLinkError org.sqlite.NativeDB._open(Ljava/lang/String;I)V 异常
- caffe - 如何修复“在图像中找不到 JPEG 数据”
- amazon-web-services - 如何使用 go-cloud s3blob 设置公共阅读?