我们如何在Swift中上传嵌套JSON参数的多部分表单数据? [英] How can we upload multipart form data with nested JSON parameters in Swift?

查看:121
本文介绍了我们如何在Swift中上传嵌套JSON参数的多部分表单数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将图像上传到服务器端点,其结构必须如下:

I need to upload an image to the server endpoint where the structure has to be as following:

 { "image": { "file": imageData }, "access_token": access_token }

我该如何发送这样的请求使用NSURLSession(或者甚至是Alamofire或AFNetworking)?

How can I send such a request using NSURLSession (or maybe even Alamofire or AFNetworking)?

推荐答案

https://stackoverflow.com/a/11092052/3871476

其他人寻找解决方案。

let manager = AFHTTPRequestOperationManager(baseURL: NSURL(string: url))
let request = manager.POST(url, parameters: param, constructingBodyWithBlock: {(formData: AFMultipartFormData!) -> Void in
    formData.appendPartWithFileData(imgdata, name: "image[file]", fileName: "photo.jpeg", mimeType: "image/jpeg")
    }, success: {(operation: AFHTTPRequestOperation!, responseObject: AnyObject!) -> Void in
        //Success
    }, failure: {(operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
        //Failure
        println(error.localizedDescription)
})

诀窍是使用image [file]参数。

这篇关于我们如何在Swift中上传嵌套JSON参数的多部分表单数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆