gpt4 book ai didi

ios - 如何使用 Swift 4 从数据中使用 Alamofire 多部分发送参数数组

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:02:12 24 4
gpt4 key购买 nike

我正在使用 Alamofire 将图像和文件上传到服务器。但是我在发送带有图像的参数数组时遇到问题。但是当我在参数中发送一个数组时,它会将数组转换为 JSON 字符串。但我想在参数中发送一个数组,而不是 JSON 字符串。我搜索了很多但没有得到任何解决方案。这是下面的代码。

let params = ["id":"101","arrayParam":["1232","12344","14325"]]

let url = www.khxjjhdfsj.com/hsdgs
let headers: HTTPHeaders = [
/* "Authorization": "your_access_token", in case you need authorization header */
"Content-type": "multipart/form-data"
]
Alamofire.upload(multipartFormData: { (multipartFormData) in
for (key, value) in params
{
multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key as String)
}
if let data = imageData
{
multipartFormData.append(data, withName: "file", fileName: fileName, mimeType: "image/png")
}
if let data = pdfData
{
multipartFormData.append(data, withName: "file", fileName: fileName, mimeType:"application/pdf")
}
}, usingThreshold: UInt64.init(), to: url, method: .post, headers: headers) { (result) in
switch result{
case .success(let upload, _, _):
upload.responseJSON { response in
print("Succesfully uploaded")
if let err = response.error
{
onError?(err)

return
}



}
case .failure(let error):
print("Error in upload: \(error.localizedDescription)")
onError?(error)
}
}

最佳答案

 let params = [
"key":value,
"key1": key2] as [String : Any]



let manager = Alamofire.SessionManager.default
manager.session.configuration.timeoutIntervalForRequest = 30000

manager.request(url, method: .post, parameters: params)
.responseJSON {


response in

switch response.result {
case .success:
stopActivityIndicator()
print(response)
if let result = response.result.value {
let JSON = result as! NSDictionary
print(JSON)
let ResponseSuccess = JSON.object(forKey: "response")!
self.displayAlert(Message: ResponseSuccess as! String, myView:self)


}


case .failure( _):

if let result = response.result.value {
stopActivityIndicator()
let JSON = result as! NSDictionary
print(JSON)
let ResponseFail = JSON.object(forKey: "response")!
self.displayAlert(Message: ResponseFail as! String, myView:self)
}
}


}

关于ios - 如何使用 Swift 4 从数据中使用 Alamofire 多部分发送参数数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51380508/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com