当前位置:  开发笔记 > 前端 > 正文

如何使用ios,swift3,Alamofire 4中的多部分表单数据将图像作为参数上传,其他参数

如何解决《如何使用ios,swift3,Alamofire4中的多部分表单数据将图像作为参数上传,其他参数》经验,为你挑选了1个好方法。

我正在使用Alamofire 4 with swift 3更新用户个人资料.我也在Router上课.我需要的是uplaod和其他参数一起成像.我可以向update用户详细介绍,无需上传图像部分.

这就是它的样子 postman

在此输入图像描述

所以有可能为此创建一个urlconvertible请求.如何使用其他参数上传图像.(这在邮递员中很好用).我怎么能用新的呢Alamofire.我尝试过如下.

let parameters = [
            "profile_image": "swift_file.jpeg"
        ]


        Alamofire.upload(multipartFormData: { (multipartFormData) in
            multipartFormData.append(UIImageJPEGRepresentation(profileImage, 1)!, withName: "file", fileName: "swift_file.jpeg", mimeType: "image/png")
            for (key, value) in parameters {
                multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
            }
        }, to:urltoUpdate)
        { (result) in
            switch result {
            case .success(let upload, _, _):
                print("the status code is :")

                upload.uploadProgress(closure: { (progress) in
                    print("something")
                })

                upload.responseJSON { response in
                    print("the resopnse code is : \(response.response?.statusCode)")
                    print("the response is : \(response)")
                }
              break
            case .failure(let encodingError):
                print("the error is  : \(encodingError.localizedDescription)")
                break
            }
        }

但这不能正常工作.希望你对这部分的帮助.



1> Bista..:

你不需要这个:

"profile_image": "swift_file.jpeg"

参数应该是:

let parameters = [
    "firstname": "Bill",
    "surname": "fox",
    //...rest of the parameters
]

这个withName: "file":

multipartFormData.append(UIImageJPEGRepresentation(profileImage, 1)!, withName: "file", fileName: "swift_file.jpeg", mimeType: "image/png")

应该是withName: "profile_image":

multipartFormData.append(UIImageJPEGRepresentation(profileImage, 1)!, withName: "profile_image", fileName: "swift_file.jpeg", mimeType: "image/png")

带标题的代码:

let parameters = [
    "firstname": "Bill",
    "surname": "fox",
    //...rest of the parameters
]

let headers = [
    "somekey": "somevalue",
    //...rest of the parameters
]

Alamofire.upload(multipartFormData: { (multipartFormData) in

    multipartFormData.append(UIImageJPEGRepresentation(profileImage, 1)!, withName: "profile_image", fileName: "swift_file.jpeg", mimeType: "image/png")

    for (key, value) in parameters {
        multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
    }

}, usingThreshold:UInt64.init(),
   to: "", //URL Here
   method: .post,
   headers: headers, //pass header dictionary here
   encodingCompletion: { (result) in

    switch result {
    case .success(let upload, _, _):
        print("the status code is :")

        upload.uploadProgress(closure: { (progress) in
            print("something")
        })

        upload.responseJSON { response in
            print("the resopnse code is : \(response.response?.statusCode)")
            print("the response is : \(response)")
        }
        break
    case .failure(let encodingError):
        print("the error is  : \(encodingError.localizedDescription)")
        break
    }
})

推荐阅读
mobiledu2402851203
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有