gpt4 book ai didi

swift - 如何在 Swift 中使用 Alamofire 处理具有不同键和多种参数的多个图像的 Multipart

转载 作者:行者123 更新时间:2023-11-30 10:37:02 24 4
gpt4 key购买 nike

如果您正在处理具有不同键的多个图像,并且还具有具有不同类型的键:值对的参数字典,例如“String”:“Any”,“String”:“[Any]”,如何快速处理多部分,[[字符串:任意]]。

let headers: HTTPHeaders = ["Content-type": "multipart/form-data"]

    Alamofire.upload(multipartFormData: { (multipartFormData) in

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

//Todo: - Images
var c = 0
for dictImage in arrImage {

let validDict = kSharedInstance.getDictionary(dictImage)

for keyName in validDict.keys {
print(keyName)

//Incr
c += 1

if let imageData = validDict[keyName] as? Data {

multipartFormData.append(imageData, withName: "\(keyName)", fileName: "\(Date().timeIntervalSince1970).jpeg", mimeType: "image/jpeg")

}

}
}


}, usingThreshold: UInt64(), to: urlString, method: .post, headers: headers) { (result) in
switch result{
case .success(let upload, _, _):
upload.responseJSON { response in
print("Succesfully uploaded")

if((response.result.value) != nil) {
debugPrint(response.result.value!)

let jsonData = JSON(response.result.value!)
if jsonData["status"].bool == true {
completion(jsonData.dictionaryObject!, true)
}else
{
completion(jsonData.dictionaryObject!, false)
}
}
}
case .failure(let error):
print("Error in upload: \(error.localizedDescription)")
failure(error, false)
}
}

最佳答案

请检查以下代码。希望对您有帮助

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

for i in 0 ..< files.count {
let fileName : String = "image\(i).jpg"
let datum : Data = files[i]
MultipartFormData.append(datum, withName: fileParamName, fileName: fileName, mimeType: "image/jpg")
}

}, to: URLString, method: .post, headers: headers) { (result) in
switch(result) {

case .success(let upload, _, _):
upload.responseJSON { response in

}
break;
case .failure(_):
}
}

关于swift - 如何在 Swift 中使用 Alamofire 处理具有不同键和多种参数的多个图像的 Multipart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57770472/

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