ios - 带参数的 Alamofire 图像上传和带补丁方法的 AuthToken header

标签 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 上传的帖子,但似乎没有一个适合我? 请帮我找到这里的问题。

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

谢谢

最佳答案

您的内容类型有误。利用 “内容类型”:“多部分/表单数据”

关于ios - 带参数的 Alamofire 图像上传和带补丁方法的 AuthToken header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50877269/

相关文章:

ios - 导入时日历中的空标题

swift - Gray Matcher 获取多个元素的文本匹配同一个灰色匹配器

swift - NSFetchedResultsController - 排序为数字但字符串字段

ios - 使用 NotificationCenter 和 Observer 在 UIViewControllers 之间发送 bool 值

swift - 无法调用非函数类型的值 'HTTPURLResponse?' - Alamofire 4.0

json - responseJSON 没有得到响应

swift - 了解 API 设计中 Swift 扩展的使用

ios - 应用程序崩溃后 xCode 设备日志未填充

iphone - 如何获取核心数据中的关系项目属性?

ios - 在横向模式下使用 Core Motion