swift - 使用 alamofire multipart 上传图像数组
问题描述
我使用 alamofire multipart 将图像数组上传到服务器它只上传第一张图像(数组计数的次数)并且不上传其余图像,当从图像选择器获取图像到将图像附加到多部分之前和之后,我会检查所有内容
extension profileViewController{
@objc private func UploadImage()
{
let headers = ["Authorization": "Bearer \(AuthService.instance.Auth_Token)"]
Alamofire.upload(multipartFormData: { multipartFormData in
if self.ImageArray?.isEmpty == false{
for (index, image) in self.ImageArray!.enumerated(){
print(index)
var imageData = image.jpegData(compressionQuality: 0.6)
print(imageData)
multipartFormData.append(imageData!, withName: "photos[]", fileName: "\(self.RandomString(length: 10)).jpeg",mimeType: "image/jpeg")
print(imageData)
imageData?.removeAll()
}
}else{
return
}
}, usingThreshold:UInt64.init(),
to: "http://couponsub.farid.azq1.com/api/save-user-photos"/*upload_imageUrl*/, //URL Here
method: .post,
headers: headers, //pass header dictionary here
encodingCompletion: { (result) in
switch result {
case .success(let upload, _, _):
print("the status code is :")
upload.uploadProgress(closure: { (progress) in
print(progress.fractionCompleted)
//self.ProgressBar.progress = CGFloat(Float(progress.fractionCompleted))
})
upload.responseJSON { response in
if response.result.isSuccess == true{
KRProgressHUD.dismiss()
}
print("the resopnse code is : \(String(describing: response.response?.statusCode))")
let json = try! JSON(data: response.data!)
print(json)
}
break
case .failure(let encodingError):
print("the error is : \(encodingError.localizedDescription)")
break
}
})
}
}
解决方案
func uploadMultipleImages(imagesData:[Data]){
Alamofire.upload(multipartFormData: { multipartFormData in
// import image to request
for imageData in imagesData {
multipartFormData.append(imageData, withName: "\(imageParamName)[]", fileName: "\(Date().timeIntervalSince1970).jpeg", mimeType: "image/jpeg")
}
for (key, value) in parameters {
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
}, to: urlString,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
}
case .failure(let error):
print(error)
}
})
}
推荐阅读
- python - 如何根据列中的值对数据框的行进行重新排序
- javascript - JavaScript 测验问题,继续点击
- tensorflow - keras:model.fit_generator 抛出错误
- python-3.x - discord.ext.commands.errors.MissingPermissions:您缺少运行此命令的静音成员权限
- flutter - 颤动父子复选框
- asp.net - 有没有办法在不更改控制器文件名的情况下更改控制器名称?
- javascript - Material-UI 的 Dialog 如何允许在对话框后面进行交互?
- git - git-multimail 排除 git diff 和后续电子邮件
- matlab - 使用 Matlab 求解使用 Euler 方法的 ODE 系统
- google-cloud-platform - 从 gcp 中的 stackdriver-agent 获取 vm 正常运行时间数据?