在swift 4中的alamofire 4中使用multipart上传图像和多个参数

时间:2018-03-02 11:29:57

标签: ios alamofire swift4 multipartform-data

我正在尝试使用带有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";
}

请帮助我!!

2 个答案:

答案 0 :(得分:0)

image/jpg没有 MIME类型。将其更改为image/jpegenter image description here

答案 1 :(得分:0)

因此,错误来自服务器端,对图像的大小限制非常小,这就是为什么它们不能保存。尝试从

更新JPEG图像的压缩
if  let imageData = UIImageJPEGRepresentation(selectedLogo, 1)

if  let imageData = UIImageJPEGRepresentation(selectedLogo, 0.6)

希望这会对你有所帮助。