swift - 如何获得在 Alamofire 中上传 base64 图像的进度?
问题描述
有什么方法可以在 alamofire 中发布带有进度的 JSON 吗?我需要做什么我有 JSON,它有 base64 图像字符串和一些其他参数,同时请求 JSON 我需要向最终用户显示进度,所以任何人都知道如何在 Alamofire 中做到这一点?
我已经按照以下链接进行操作,但它在 Alamofire 中给了我一个语法错误?
let parameters: [String: AnyObject] = ["key": "value" as AnyObject]
let mutableURLRequest = NSMutableURLRequest(url: URL(string: "url goes here")!)
mutableURLRequest.httpMethod = "POST"
let encodedURLRequest = try! Alamofire.URLEncoding.default.encode(mutableURLRequest as! URLRequestConvertible, with: parameters)
let data = encodedURLRequest.httpBody!
Alamofire.upload(mutableURLRequest, data)
.progress { _, totalBytesRead, totalBytesExpectedToRead in
print("ENTER .PROGRESSS")
print("\(totalBytesRead) of \(totalBytesExpectedToRead)")
}
.responseJSON { _, _, mydata, _ in
print(mydata)
}
它给了我以下错误
无法使用类型为“(NSMutableURLRequet,数据)的参数列表调用“上传”
解决方案
如果要显示进度 hud,可以使用以下代码:
let binaryImageData = base64EncodeImage(image)
SVProgressHUD.show() //use any progress hud
let parameters : [String:Any] = [
"Name" : "UserProfilePhoto",
"Body": binaryImageData,
"parentId": userId
]
let header = ["content-type" : "application/json", "Authorization": "your token here"]
var imageURL: URL {
return URL(string: "Your UrlString here")
}
Alamofire.request(imageURL, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: header).responseJSON {
response in
if (response.result.isSuccess){
SVProgressHUD.dismiss()
let result = response.result.value as? NSDictionary
}
else{
SVProgressHUD.dismiss()
}
}
}
推荐阅读
- python - 当多个线程访问一个类变量时,该变量是所有线程共有的吗?
- c# - 在 EventSourcing 中,关于订阅的公认智慧是什么?
- javascript - 使用 Redux 在 Axios 调用后更新 React 组件状态
- eiffel - 埃菲尔:多个通用约束的语法错误
- mongodb - 如何验证在连接级别设置的 mongoDB 选项
- angular - ngx-quill / quill.js 自定义印迹操作
- python - 在对帧进行排序时使用 try 语句
- angular - 有什么方法可以调试为什么角度路线没有被识别?
- c# - 使用转换器更改所有列的背景?
- json - i18next 翻译外部组件