您知道如何使用RxAlamofire上传multipartFormdata吗? 现在我正在使用此解决方案,这只是一个包装好的Alamofire请求,可能还有一些更优雅的方法吗?
func uploadImage(image: UIImage, url: String) -> Observable<String>{
return Observable<String>.create({ observer in
let parameters: [String:String] = [:]
let headers = ["x-auth-token": "7tw8t78twet"]
Alamofire.upload(multipartFormData: { multipartFormData in
if let imageData = UIImageJPEGRepresentation(image, 0.8) {
multipartFormData.append(imageData, withName: "file", fileName: "file.jpg", mimeType: "image/jpg")
}
for (key, value) in parameters {
multipartFormData.append((value.data(using: .utf8))!, withName: key)
}}, to: url, method: .post, headers: headers,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { [weak self] response in
guard self != nil else { return }
guard response.result.error == nil else {
observer.onError(response.result.error!)
return
}
if let value: Any = response.result.value {
observer.onNext(JSON(value)["path"].stringValue)
}
observer.onCompleted()
}
case .failure(let encodingError):
observer.onError(encodingError)
}
})
return Disposables.create();
})
}