使用Alamofire通过参数数组上传多个文件 [英] Multiple file upload with array of parameters using Alamofire

查看:874
本文介绍了使用Alamofire通过参数数组上传多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有参数数组和图像数组,每组参数包含一个且只有一个image.my代码

i have array of parameters and array of images each set of parameter contain one and only one image.my code

let imgData = UIImageJPEGRepresentation(imageView.image!, 0.2)!
  Alamofire.upload(multipartFormData: { multipartFormData in
    multipartFormData.append(imgData, withName: "fileset",fileName: "file.jpg", mimeType: "image/jpg")

      for (key, value) in params {
        multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
      }
    },
                   to:URLUpdateProfile,
                   method:.post,
                   headers:headers)
    { (result) in
      switch result {
      case .success(let upload, _, _):

        upload.uploadProgress(closure: { (progress) in
          print("Upload Progress: \(progress.fractionCompleted)")
        })

        upload.responseJSON { response in
          print(response.result.value)
        }

      case .failure(let encodingError):
        print(encodingError)  
      }
    }

使用此代码我能够至上传一个图像和一个参数。但我想发送数组中的参数和数组中的图像。是上传图像数组的参数数组的方法吗?如果是,如何跟踪图像和参数?

with this code i am able to upload one image along with one parameter.but i want to send parameter in array and image in array too.is the way to upload array of params with array of image? if yes how to track image and parameter?

推荐答案

您可以在中上传每个图像及其参数操作。您的操作应如下所示:

You can upload each image and its param in an Operation. Your Operation should look something like this:

class UploadImageOperation: Operation {
private var image: UIImage

init(withImage: UIImage) {
    super.init()

    image = withImage
}

override func main() {
    let imgData = UIImageJPEGRepresentation(image, 0.2)!
    Alamofire.upload(multipartFormData: { multipartFormData in
        multipartFormData.append(imgData, withName: "fileset",fileName: "file.jpg", mimeType: "image/jpg")

        for (key, value) in params {
            multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
        }
    },
                     to:URLUpdateProfile,
                     method:.post,
                     headers:headers)
    { (result) in
        switch result {
            case .success(let upload, _, _):

                upload.uploadProgress(closure: { (progress) in
                    print("Upload Progress: \(progress.fractionCompleted)")
                })

                upload.responseJSON { response in
                    print(response.result.value)
                }

            case .failure(let encodingError):
                print(encodingError)  
            }
        }
    }
}

然后你创建操作并将它们添加到队列中,如下所示:

Then you create the operations and add them to the queue like this:

let opQueue = OperationQueue()
opQueue.name = "imageUploadQueue"
opQueue.maxConcurrentOperationCount = 5 //number of images you want to be uploaded simultaneously
opQueue.qualityOfService = .background

for image in arrayOfImages {
    let uploadImageOperation = UploadImageOperation(withImage: image)
    opQueue.addOperation(uploadImageOperation)
}

这篇关于使用Alamofire通过参数数组上传多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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