我想POST使用alamofire 5.5的多部分形式的图像文件我成功地用最近的alamofire实现了GET方法,但却被POST方法卡住了。
TIA-
为了将图像上传到服务器,我们需要使用多部分请求,您可以使用以下代码片段将图像上传至服务器。
func uploadImage(imgType:String,imgData:Data,imageName:String){
// params to send additional data, for eg. AccessToken or userUserId
let params = ["userID":"userId","accessToken":"your accessToken"]
print(params)
AF.upload(multipartFormData: { multiPart in
for (key,keyValue) in params{
if let keyData = keyValue.data(using: .utf8){
multiPart.append(keyData, withName: key)
}
}
multiPart.append(imgData, withName: "key",fileName: imageName,mimeType: "image/*")
}, to: "Your URL",headers: []).responseJSON { apiResponse in
switch apiResponse.result{
case .success(_):
let apiDictionary = apiResponse.value as? [String:Any]
print("apiResponse --- (apiDictionary)")
case .failure(_):
print("got an error")
}
}
}