Alamofire: How to show upload multipartFormData progress?

Created on 27 Jun 2017  路  3Comments  路  Source: Alamofire/Alamofire

I knew I can do this for normal upload:

let fileURL = Bundle.main.url(forResource: "data", withExtension: "zip")

Alamofire.upload(fileURL!, to: "http://localhost:3000/upload")
    .uploadProgress { progress in // main queue by default
        print("Upload Progress: \(progress.fractionCompleted)")
    }
    .responseJSON { response in
        debugPrint(response)
}

But how am I supposed to show progress for uploading multipartFormData?

Alamofire.upload(multipartFormData: { (multipartFormData) in
  let path = "\(Bundle.main.resourcePath!)/images"
  let all = try? FileManager.default.contentsOfDirectory(at: URL(string: path)!, includingPropertiesForKeys: nil, options: [])

  let filtered = all!.filter { $0.pathExtension == "jpg" }
  for item in filtered {
      let url = URL(fileURLWithPath: item.path, isDirectory: false)
      multipartFormData.append(url, withName: "photos", fileName: url.lastPathComponent, mimeType: "image/jpeg")
  }
}, to: "http://localhost:3000/upload") {
  (encodingResult) in
  switch encodingResult {
  case .success(let upload, _, _):
      upload.responseJSON { response in
          debugPrint(response)
      }
  case .failure(let encodingError):
      print(encodingError)
  }
}
support

Most helpful comment

Alamofire.upload(multipartFormData: { (multipartFormData) in
    ...
}, to: "http://localhost:3000/upload") {
  (encodingResult) in
  switch encodingResult {
  case .success(let upload, _, _):
      upload.uploadProgress(closure: { (progress) in
           print("Upload Progress: \(progress.fractionCompleted)")
      })
      upload.responseJSON { response in
          debugPrint(response)
      }
  case .failure(let encodingError):
      print(encodingError)
  }
} 

All 3 comments

Alamofire.upload(multipartFormData: { (multipartFormData) in
    ...
}, to: "http://localhost:3000/upload") {
  (encodingResult) in
  switch encodingResult {
  case .success(let upload, _, _):
      upload.uploadProgress(closure: { (progress) in
           print("Upload Progress: \(progress.fractionCompleted)")
      })
      upload.responseJSON { response in
          debugPrint(response)
      }
  case .failure(let encodingError):
      print(encodingError)
  }
} 

Thank you so much. It works.
Anyway to show progress of normal http request?

Sorry, we use our GitHub project for bug reports and feature requests. In the future, you should open questions like this on Stack Overflow and tag alamofire.

Cheers. 馃嵒


From our Contribution Guidelines

Asking Questions

We don't use GitHub as a support forum. For any usage questions that are not specific to the project itself, please ask on Stack Overflow instead. By doing so, you'll be more likely to quickly solve your problem, and you'll allow anyone else with the same question to find the answer. This also allows maintainers to focus on improving the project for others.

Was this page helpful?
0 / 5 - 0 ratings