记录开发:上传文件和普通的json数据上传不一样,特别注意fileName,我因为fileName里的名字,后台得不到图片数据,或者是 图片数据得到不正确,很大可能是因为fileName 不正确,特意记录下,我搞了好久,记录自己,也提醒下遇到同样问题的同学。
let fileName = Date().string(withFormat: "yyyymmddHHmmss") + ".jpg"
RxAlamofire.upload(multipartFormData: {
$0.append(data, withName: "image", fileName: fileName, mimeType: "image/jpg")
//添加其他参数
$0.append(bodyData!, withName: "body")
},
to: target.url,
method: .post,
headers: HTTPHeaders(Self.header)) // HTTPHeaders(header)
.flatMap {
$0.rx.responseString(encoding: .utf8)
}
.subscribe(onNext: { _, response in
Log("--- [response] --,\(response)")
// if let dict = response as? [String: Any] {
var jsonDecoding = JSONDecodingOptions()
jsonDecoding.ignoreUnknownFields = true
guard let model = try? type.self.init(jsonString: response, options: jsonDecoding) else {
return
}
signal.onNext(model)
// }
}, onError: { error in
Log("---[error]--\(error)")
signal.onError(error)
})
.disposed(by: disposeBag)
return Disposables.create()
网友评论