Alamofire 4文件上传参数问题

时间:2017-03-21 18:35:56

标签: ios swift xcode afnetworking alamofire

请看邮递员的照片 POSTMAN success result 但代码没有运行我错过了一些请帮帮我

 var parameters = [String: String]()
        parameters = [
            "profile_image": "imagefile"
        ]

        let headers: HTTPHeaders = [
            "Accept": "application/json"
        ]

        let url = "http://******/public/api/v1/profile/update-picture?api_token=" + "aySlC26ZTHtlnS0lhUpdghxkd9gKJBLXLYFUO2Jidmiisoka9iFIicwRIZFx"
        let completeUrl = URL(string:url)!

        let imageData = UIImageJPEGRepresentation(chosenImage!, 1)
        print ("image data:: \(imageData)")
        print ("chosenImage:: \(chosenImage)")

//        Alamofire.upload(imageData!, to: completeUrl).response { response in
//            print (response)
//        }

        Alamofire.upload(
            multipartFormData: { 
                multipartFormData in
                multipartFormData.append(imageData!,
                                         withName: "imagefile",
                                         fileName: "image.jpg",
                                         mimeType: "image/jpeg")
                for (key, value) in parameters {
                    multipartFormData.append(value.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!, withName: key)
                }
        },
            to: completeUrl,
            headers: headers,
            encodingCompletion: { encodingResult in
                switch encodingResult {
                case .success(let upload, _, _):
                    upload.responseJSON{ response in
                        print(response)
                    }
                case .failure(let encodingError):
                    print(encodingError)
                }
            }
        )

Postman Response 在邮递员上取得成功但是头部有问题,你能否指出我犯下的愚蠢错误

1 个答案:

答案 0 :(得分:0)

我相信你需要以这种方式附加图片

multipartFormData.append(imageData!,
    withName: "profile_image",
    fileName: "image.jpg",
    mimeType: "image/jpeg")

您无需设置或包含parameters = ["profile_image": "image file"]

正如@rob所提到的那样,使用Charles并将Postman的请求与来自模拟器的请求进行比较。它将有助于确定不同的内容

相关问题