gpt4 book ai didi

ios - 如何使用Alamofire上传多张图片?

转载 作者:行者123 更新时间:2023-11-29 05:35:57 25 4
gpt4 key购买 nike

这是我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com