【问题标题】:upload image and multiple parameters using multipart in alamofire 4 in swift 4在swift 4中使用alamofire 4中的multipart上传图像和多个参数
【发布时间】:2018-08-10 14:46:11
【问题描述】:

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    所以错误来自服务器端,图像的大小限制非常小,这就是它们没有保存的原因。尝试从

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

    if  let imageData = UIImageJPEGRepresentation(selectedLogo, 0.6)
    

    希望这会对你有所帮助。

    【讨论】:

    • 我更改了压缩值但无法上传图片
    • 是相同的响应获得成功状态代码(200)但同样发生错误。
    • 应该可以。你可以看到这个解决方案来解决这个问题 - stackoverflow.com/questions/43972651/…
    猜你喜欢
    • 2019-03-16
    • 1970-01-01
    • 1970-01-01
    • 2017-04-22
    • 2017-07-14
    • 1970-01-01
    • 2018-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多