使用参数和AuthToken标头上传Alamofire图像并使用补丁方法

时间:2018-06-15 14:08:27

标签: ios swift alamofire image-uploading

这里是我正在使用的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上传,但似乎没有一个对我有用吗? 请帮我在这里找到问题。

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

谢谢

1 个答案:

答案 0 :(得分:0)

您的内容类型有误。使用 “内容类型”:“多部分/格式数据”