如何使用Alamofire上传多张图片? [英] How to upload multiple images using Alamofire?

查看:517
本文介绍了如何使用Alamofire上传多张图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的CURL的样子:

Here is what my CURL looks like:

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"

文本上传使用以下代码进行操作:

The text uploading works with the following code:

 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)
            }})}

如何更改我的代码以上传多个图像?

How to change my code to make possible multiple image upload?

推荐答案

使用递归:-

let noOfImage = 5
var coutUpload = 0
func uploadData(){
    Alamofire.upload(multipartFormData: { multipartFormData in
    }, usingThreshold:UInt64.init(),
       to: uploadUrl,
       method: .post,
       headers: header,
       encodingCompletion: {(result) in
        switch result{
        case .success(let upload, _, _):
            upload.responseJSON(completionHandler: { (res) in
                if res.result.isSuccess{
                    if coutUpload == noOfImage{
                        //Uploaded All Images sucessfully
                    }else{
                        coutUpload += 1
                        uploadData()
                    }

                }else{
                    //ERROR SHOW
                }
            })
            break
        case .failure(let encodingError):
            print("the error is  : \(encodingError.localizedDescription)")

            break
        }
    })
}

这篇关于如何使用Alamofire上传多张图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆