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

问题描述 投票:0回答:2

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

请帮我!!

ios alamofire swift4 multipartform-data
2个回答
0
投票

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


0
投票

因此错误来自服务器端,对图像的大小限制非常小,这就是他们没有保存的原因。尝试更新JPEG图像的压缩

if  let imageData = UIImageJPEGRepresentation(selectedLogo, 1)

if  let imageData = UIImageJPEGRepresentation(selectedLogo, 0.6)

希望这会帮助你。

© www.soinside.com 2019 - 2024. All rights reserved.