直接上代码
extension HttpRequestUtil {
//MARK: 上传多张图片 -
func uploadImageWith(urlType: HttpURLType,url:String, type:String = "avatar", imgs:[UIImage],handler:BlockWithParameters<String>?){
guard let URL = URL(string: urlType.url + url) else {return}
SVProgressHUD.show(withStatus: "上传中")
qmSession.upload(multipartFormData: { (formData) in
for (index,obj) in imgs.enumerated(){
let formatter = DateFormatter()
formatter.dateFormat = "yyyyMMddHHmmss"
let fileName = formatter.string(from: Date()) + (UserUtil.share.userInfo?.user_id ?? "") + "\(index)" + ".jpeg"
PLog(fileName)
if let imageData = obj.jpegData(compressionQuality: 0.8) {
//withName:和后台服务器的name要一致 ;fileName:可以充分利用写成用户的id,但是格式要写对; mimeType:规定的,要上传其他格式可以自行百度查一下
formData.append(imageData, withName: "file",fileName: fileName, mimeType: "image/jpeg")
//avatar头像,id_card身份证,face_recognition人脸视频
formData.append(type.data(using: String.Encoding.utf8)!, withName: "type")
}
}
}, to: URL , method: .post, headers: headers).response { relust in
switch relust.result{
case .success(let upload):
if let json = CommonUtil.jsonData(data: upload!) as? [String : Any] {
PLog("---->上传成功\(json)")
if let dic = json["data"] as? [String : Any] , let url = dic["url"] as? String{
SVProgressHUD.dismiss()
handler?(url)
}else{
SVProgressHUD.showError(withStatus: json["msg"] as? String)
}
}else{
PLog(upload)
SVProgressHUD.showError(withStatus: "上传失败")
}
break
case .failure(let error):
PLog(error.localizedDescription)
SVProgressHUD.dismiss()
break
}
}
}
//MARK: 上传视频文件
func uploadVideoWith(urlType: HttpURLType,url:String, outFilePath:String,handler:BlockWithParameters<String>?){
let outFileVideoUrl = URL(fileURLWithPath: outFilePath)
guard let URL = URL(string: urlType.url + url) else {return}
SVProgressHUD.show(withStatus: "上传中")
qmSession.upload(multipartFormData: { (formData) in
let formatter = DateFormatter()
formatter.dateFormat = "yyyyMMddHHmmss"
let fileName = formatter.string(from: Date()) + (UserUtil.share.userInfo?.user_id ?? "") + ".mp4"
let videoData = try? Data(contentsOf: outFileVideoUrl)
//withName:和后台服务器的name要一致 ;fileName:可以充分利用写成用户的id,但是格式要写对; mimeType:规定的,要上传其他格式可以自行百度查一下
if let video = videoData {
formData.append(video, withName: "file",fileName: fileName, mimeType: "video/mp4")
//avatar头像,id_card身份证,face_recognition人脸视频
formData.append("face_recognition".data(using: String.Encoding.utf8)!, withName: "type")
}
}, to: URL , method: .post, headers: headers).response { relust in
switch relust.result{
case .success(let upload):
if let json = CommonUtil.jsonData(data: upload!) as? [String : Any] {
if let dic = json["data"] as? [String : Any],let url = dic["url"] as? String{
SVProgressHUD.dismiss()
handler?(url)
}else{
SVProgressHUD.showError(withStatus: json["msg"] as? String)
}
}else{
PLog(upload)
SVProgressHUD.showError(withStatus: "上传失败")
}
break
case .failure(let error):
PLog(error.localizedDescription)
SVProgressHUD.dismiss()
break
}
}
}
}
使用
//图片测试 type 是后端参数
HttpRequestUtil.share.uploadImageWith(urlType: .common,url: uploadURl, type: "", imgs: [img,img2]){ data in
PLog(data)
}
//视频测试 outFilePath 传递视频path 即可
if let path = Bundle.main.path(forResource: "775.MP4", ofType: nil){
HttpRequestUtil.share.uploadVideoWith(urlType: .common, url: uploadURl, outFilePath: path){data in
PLog(data)
}
}