gpt4 book ai didi

swift - 如何在 swift 中使用字典设置多部分 "withname"?

转载 作者:行者123 更新时间:2023-11-30 10:48:05 25 4
gpt4 key购买 nike

我想以多部分形式将图像发送到服务器。普通的 图像上传正在与多部分一起使用,但面临 问题是当我需要使用字典内的数组设置 withname 时。

  alamofireManager.upload(multipartFormData: { multipartFormData in

for i in 0..<images.count {

let imgData = UIImagePNGRepresentation(images[i])!

multipartFormData.append(imgData, withName: "fileUpload",fileName: "\(images)i", mimeType: "image/png")

}

服务器参数就像

 "documants" : [{
"documentType" : "Image",
"fileUpload: "" // multipart data
},{
"documentType" : "Image",
"fileUpload: "" // multipart data
}]

那么如何使用 multipartFormData 来提及节点名称 documents[0].fileUploadwithName 呢?

最佳答案

您可以通过这种方式实现“multipart”...使用 Swift4.2

let headers: HTTPHeaders = [
/* "Authorization": "your_access_token", in case you need authorization header */
"Content-type": "multipart/form-data"
]

let url = try! URLRequest(url: baseURL, method: .post, headers: headers)

Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(img, withName: "file", fileName: imgName, mimeType: "\(fileType ?? "jpg")")
}, with: url) { result in
switch result {
case .success(let upload, _, _):
upload.responseString { response in
switch (response.response?.statusCode)
{
case 200: //The request was fulfilled
print("Network - HandShaking Successfull...!!!")
debugPrint(response)
}

case .failure(let encodingError):
print(encodingError)
}
}

希望您能了解“多部分”......!!!

关于swift - 如何在 swift 中使用字典设置多部分 "withname"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55350215/

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