【问题标题】:Alamofire Image upload with Parameters and AuthToken Header with patch method带有参数的 Alamofire 图像上传和带有补丁方法的 AuthToken 标头
【发布时间】:2018-11-25 09:34:40
【问题描述】:

这是我正在使用的 alamofire 代码

params 是字典[String:Any]

Alamofire.upload(
    multipartFormData: { MultipartFormData in

        for (key, value) in params {
            if let image = value as? UIImage {
                if let imageData = UIImageJPEGRepresentation(image, 0.2) {
                    MultipartFormData.append(imageData, withName: "image", fileName: "file.jpg", mimeType: "image/jpg")
                }
            }else {
                MultipartFormData.append(String(describing: value).data(using: String.Encoding.utf8)!, withName: key)
            }
        }

}, to:url, method: .patch, headers: ["token": authToken,"Content-Type":"application/json"]) { (result) in

    switch result {
    case .success(let upload, _, _):
        upload.responseJSON { response in
            guard response.result.isSuccess else {
                print(response.error?.localizedDescription ?? "Error while requesting")
                return
            }
            if let value = response.result.value {
                let json = JSON(value)
            }
        }

    case .failure(let encodingError):
        print(encodingError)
    }
}

我想将图像上传到服务器,阅读一些通过 alamofire 上传的帖子,但似乎没有一个对我有用? 请帮我在这里找到问题。

重复问题中给出的解决方案对我不起作用

谢谢

【问题讨论】:

标签: ios swift alamofire image-uploading


【解决方案1】:

您的内容类型错误。采用 "Content-Type":"multipart/form-data"

【讨论】:

  • 为什么要使用补丁法?可以试试发帖吗?
  • api 开发者已将其设置为补丁 :(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 2018-03-09
  • 1970-01-01
  • 2017-07-14
  • 2013-08-02
相关资源
最近更新 更多