Swift Alamofire 带参数 头像上传 ,
// 头像修改
func headerUpdateRequest(){
let parameters:NSMutableDictionary = NSMutableDictionary()
parameters.setValue(token, forKey: "token")
parameters.setValue(telephone, forKey: "telephone")
showWatting()
let url = URL(string: PATH_INFOUPDATE)
Alamofire.upload(multipartFormData: { (multipartFormData) in
let _data = UIImageJPEGRepresentation((self.headerImage?.image)!, 0.5)
let dateFormatter:DateFormatter = DateFormatter();
dateFormatter.dateFormat = "yyyyMMddHHmmss"
let str = dateFormatter.string(from:NSDate() as Date)
multipartFormData.append(_data!, withName: "headimg", fileName: "\(str).jpg", mimeType: "image/jpeg")
//遍历字典
for (key, value) in parameters {
let str:String = value as! String
let _datas:Data = str.data(using: String.Encoding.utf8)!
multipartFormData.append(_datas, withName: key as! String)
}
}, to: url!) { (result) in
switch result {
case .success(let upload, _, _):
upload.responseJSON(completionHandler: { (response) in
self.hideToast()
if let value = response.result.value {
let json = JSON(value)
if json["success"] == true {
}
self.showToast(message:"\(json["msg"])")
}
})
case .failure:
self.hideToast()
self.showToast(message:"网络异常")
}
}
}