[英]Alamofire: how to append a json array parameter in a multipart form data?
I'm tryna send a photo along with parameters, but the catch is that I want to send a JSON array to the server. 我尝试发送照片和参数,但问题是我想将JSON数组发送到服务器。 It seems Alamofire doesn't have a method to send a list of Data
, so what's another good alternative to this? 似乎Alamofire没有发送Data
列表的方法,那么另外一个很好的选择呢?
the key part of the problem is: 问题的关键部分是:
var encodedTags: [Data] = tags.map({ return $0.data(using: .utf8)!})
mpd.append(encodedTags, withName: key)
within this upload call: 在此上传电话中:
let parameters: [String: Any] = ["username": "TheCooliest", ..., "tags": ["KoolKid", "TheKooliest", "BetterThanKimK"]
...
upload(multipartFormData: { (mpd) in
mpd.append(url, withName: "file", fileName: "weeknd.jpg")
for (key, value) in parameters {
if let tags = value as? [String], key == "tags" {
var encodedTags = tags.map({ return $0.data(using: .utf8)!})
mpd.append(encodedTags, withName: key)
}
}
}
If you want to do that with Swift 4, you could use the new JSONEncoder. 如果你想用Swift 4做,你可以使用新的JSONEncoder。 You could do something like this to upload a file and a JSON parameter using multipart form data: 您可以使用多部分表单数据上传文件和JSON参数:
let image = UIImage(named: "test")
let png = UIImagePNGRepresentation(image!)!
let arr = ["str1", "str2"]
let jsonArr = try? JSONEncoder().encode(arr)
Alamofire.upload(multipartFormData: { (multiPart) in
multiPart.append(png, withName: "file", fileName: "aaa.png", mimeType: "image/png")
if let jsonArr = jsonArr {
multiPart.append(jsonArr, withName: "pictures")
}
}, to: URL) { (result) in
}
I had a same situation, you have to convert your array to string then encode and upload. 我有同样的情况,你必须将你的数组转换为字符串然后编码和上传。 In my case i had to encrypt the array and send to server then server decrypts the array. 在我的情况下,我不得不加密数组并发送到服务器,然后服务器解密数组。
var encodedTags = tags.map({ return $0})
//write logic to convert array to string
mpd.append(encodedTags.data(using: .utf8)!, withName: key)
Here is the Answer for Multipart request, Please review code. 这是Multipart请求的答案,请查看代码。
Alamofire.upload(multipartFormData:{ multipartFormData in
let firstNameTxt = self.firstNmae.text!.data(using: .utf8)
multipartFormData.append(firstNameTxt!, withName: "first_name", mimeType: "text/plain")
},
usingThreshold:UInt64.init(), to:AppConstant.GlobalConstants.updateProfile, method:.post, headers:["Authorization": "auth_token"],encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
}
case .failure(let encodingError):
print(encodingError)
}
})
Okay, so I used JSONSerialization
. 好的,所以我使用了JSONSerialization
。 It converts my list into an Any
object which I convert into Data. 它将我的列表转换为Any
对象,我将其转换为Data。
for (key, value) in parameters {
if let tags = value as? [String], key == "tags" {
do {
let json = try JSONSerialization.data(withJSONObject: tags, options: .prettyPrinted)
mpd.append(json as Data, withName: key)
} catch {}
}
You can do as below : 你可以这样做:
Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(imageData, withName: "xyz", fileName: "file.jpeg", mimeType: "image/jpeg") // append data what you want
}, to: url)
{ (result) in
//result
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.