我正在尝试在服务器上进行视频上传。我正在使用Alamofire。我正在接近一个解决方案,但是...我有一个多部分数据表单方法,其中包含这样的参数“param1”:“test”,“param2”:[“1”,“2”],param3:{“测试“:”123“}
param1仍然放置一个函数Alamofire.upload:
multipartFormData.appendBodyPart (date "test" .dataUsingEncoding (NSUTF8StringEncoding, allowLossyConversion: false) !, name: "param1")
如何添加param2和param3?
有我的方法:
Alamofire.upload(
.POST,
url,
headers: headers,
multipartFormData: { multipartFormData in
multipartFormData.appendBodyPart(data: "test".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"param1")
//it is only visualization what I want to do. I know that it isn't working.
//multipartFormData.appendBodyPart(data: ["1", "2"].dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"param2")
//multipartFormData.appendBodyPart(data: {"test": "123 "}.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"param3")
let fileURL = DataManager().getFileDirectionForFile("movie.mp4")
multipartFormData.appendBodyPart(fileURL: fileURL, name: "file")
},
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseString { response in
debugPrint(response)
}
case .Failure(let encodingError):
print(encodingError)
}
}
答案 0 :(得分:5)
首先创建param dict以传入请求。
let array = ["1","2"]
let dict = [
"test" : "123"
]
let params = [
"param1" : "test",
"param2" : array,
"param3" : dict
]
然后将该params变量传递给Alamofire.upload
Alamofire.upload(.POST, url, multipartFormData: { (multipartFormData) in
multipartFormData.appendBodyPart(data: params["param1"]!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name: "param1")
multipartFormData.appendBodyPart(data: params["param2"]!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name: "param2")
multipartFormData.appendBodyPart(data: params["param3"]!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name: "param3")
}) { (encodingResult) in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseString { response in
debugPrint(response)
}
case .Failure(let encodingError):
print(encodingError)
}
}
答案 1 :(得分:0)
这取决于您的API。我的API在相同的情况下可以处理这种格式:
multipartFormData.appendBodyPart(data: "1".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"param2[0]")
multipartFormData.appendBodyPart(data: "2".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"param2[1]")
multipartFormData.appendBodyPart(data: "123 ".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"param3[test]")
也许也适合你。