我尝试了太多时间,使用了很多方法,但是无法将图像上传到服务器,这让我知道我做错了,但是无法将图像上传到服务器。
func callsendImageAPI(){
let headers: HTTPHeaders
headers = ["Content-type": "multipart/form-data",
"Accept-Language": ApiService.instance.defaults[.langugaeCode] ?? "en",
"Authorization": "Bearer " + ApiService.instance.getToken()
]
let url = BASE_URL + "uploadImages"
AF.upload(multipartFormData: { (multipartFormData) in
for item in self.carImages.enumerated() {
switch item.element {
case .photo(let photo):
let image = photo.image
multipartFormData.append(image.jpegData(compressionQuality: 1)!, withName: "images[0]", fileName: "file.jpeg", mimeType: "image/jpeg")
default:
break
}
}
},to: url, usingThreshold: UInt64.init(),
method: .post,
headers: headers).response{ response in
if((response.error != nil)){
do{
if let jsonData = response.data{
let jsonDecoder = JSONDecoder()
let JSON = try jsonDecoder.decode([RegularPostImage].self, from: jsonData)
print(JSON)
}
}catch{
print("error message")
}
}else{
print(response.error)
}
}
}
预先感谢