项目的上传图片协议,包含多种参数,其中图片必须放入指定参数,以下是使用Alamofire解决此问题方法:
Alamofire.upload(.POST, urlStr, headers: headers,
multipartFormData: { (multipartFormData) in
// 图片绑定参数
let imageName = String(NSDate()) + ".png"
multipartFormData.appendBodyPart(data: fileData, name: "file_data", fileName: imageName, mimeType: "image/png")
// 其余参数绑定
for (key, value) in parameters! {
assert(value is String)
multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)
}
},
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseString(completionHandler: { (response) in
//
})
break
case .Failure(let error):
//
break
}
}
)