作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的 CURL 的样子:
curl -X POST "http://localhost:2202/api/project" -H "accept: aplication/json" -H "key: x" -H "Content-Type: multipart/form-data" -F "project={"title":"Test Title","description":"Test description for new project","priority":false,"category_id":1,"location_id":1}" -F "images[]=@fileName.jpg;type=image/jpeg"
文本上传使用以下代码:
let parametersText = ["project":["title":requestName.text!,"description":requestDescription.text!,"priority":emergencySwitch.isOn,"category_id":selectedCategoryID,"location_id":selectedLocationID]]
var selectedImagesForUpload = [Image]()
Alamofire.upload(multipartFormData: { multipartFormData in
// Texts
for (key, value) in parametersText{
do{
let data = try JSONSerialization.data(withJSONObject: value, options: .prettyPrinted)
multipartFormData.append(data, withName: key)
}catch(let err){
print(err.localizedDescription)
}
}
},usingThreshold:UInt64.init(),
to: url,
method: .post,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
print(response.result.value)
sender.isLoading = false
self.showNotification(color: #colorLiteral(red: 0.1803921569, green: 0.8, blue: 0.4431372549, alpha: 1), title: "Success", icon: #imageLiteral(resourceName: "UploadSuccess"))
}
case .failure(let encodingError):
print(encodingError)
}})}
如何更改我的代码以实现多个图像上传?
最佳答案
class func uploadImage(showloader : Bool,imgData : [Data],url : URL,parameters : [String : Any],success:@escaping (JSON) -> Void, failure:@escaping () -> Void ) {
if Reachability()?.isReachable == true{
if (showloader) {
SVProgressHUD.show()
}
Alamofire.upload(multipartFormData: { multipartFormData in
for imageData in imgData{
multipartFormData.append(imageData, withName: "car_image_name[0]",fileName: "\(NSDate().timeIntervalSince1970).jpg", mimeType: "image/jpg")
}
for (key, value) in parameters {
multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
}
},
to: baseURL + url)
{ (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})
upload.responseJSON { response in
guard let responseValue = response.result.value else {
if (showloader) {
SVProgressHUD.dismiss()
}
failure()
return
}
print(responseValue)
let responseJson = JSON(responseValue)
let status = CheckForSuccessApi(data: responseJson)
let resJson = JSON(response.result.value!)
switch status {
case true:
if (showloader) {
SVProgressHUD.dismiss()
}
success(resJson)
default:
print(responseJson.dictionaryObject!)
if let message = responseJson.dictionaryObject?[ApiKeys.result] as? NSDictionary{
DELEGATE.window?.rootViewController?.showAlertView(alertTitle: stringConstants.alerts.Error, msgString: message.value(forKey: ApiKeys.message) as! String)
}
if (showloader) {
SVProgressHUD.dismiss()
}
failure()
}
}
case .failure(let encodingError):
print(encodingError)
let error : Error = encodingError
if (showloader) {
SVProgressHUD.dismiss()
}
failure()
}
}
}else{
//display no internet available message
DELEGATE.window?.rootViewController?.showAlertView(alertTitle: stringConstants.alerts.NetworkError, msgString: stringConstants.alerts.NoInternetMsg)
}
}
关于ios - 如何使用Alamofire上传多张图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57040554/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!