我正在尝试使用带有swift 4的alamofire multipart上传带有多个参数的图像,但我无法成功上传图片,我得到了回应
{
"error" : "error uploading file, please retry"
}
这是我在上传按钮事件中调用的函数。
// selectedLogo = (info["UIImagePickerControllerOriginalImage"] as? UIImage)!
let imageData = UIImageJPEGRepresentation(selectedLogo, 1.0)
let parameters: Parameters = ["id": strUserId,
"telephone": self.txtTelephoneNumber.text!,
"email": self.txtEmail.text!,
"notice":self.txtNotices.text!,
"status" : userData["status"]]
print(parameters)
Alamofire.upload(multipartFormData: { (multipartFormData) in
if let data = imageData{
multipartFormData.append(data, withName: "club_image", fileName: "file.jpg", mimeType: "image/jpg")
}
for (key, value) in parameters {
multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!,withName: key as String)
}
}, to:"\(self.app.strBaseAPI)updatedata.php")
{ (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
//Print progress
print(progress)
})
upload.validate()
upload.responseJSON { response in
if response.response?.statusCode == 200
{
if response.result.isSuccess == true
{
if let value = response.result.value
{
self.json = JSON(value)
print(self.json)
let strStatus : String = self.json["success"].stringValue
if strStatus == "true"
{
Toast(text: strStatus).show()
}else{
Toast(text: strStatus).show()
}
}else{
Toast(text: "Something wrong").show()
}
}else{
Toast(text: "Something wrong").show()
}
}else{
SVProgressHUD.dismiss()
Toast(text: "Something wrong").show()
}
}
case .failure(let encodingError):
//print encodingError.description
print(encodingError.localizedDescription)
}
当我使用UIImagePNGRepresentation转换图像时使用相同的方法只需更改一行
multipartFormData.append(imageData!,withName:“image”,fileName: “image.png”,mimeType:“image / png”)
它会给我这样的
SUCCESS: {
error = "error data post";
}
请帮助我!!