【问题标题】:Add progress to file uploading using Alamofire使用 Alamofire 为文件上传添加进度
【发布时间】:2016-08-11 06:14:49
【问题描述】:

如何获取文件上传进度?

// import Alamofire
  func uploadWithAlamofire() {
    let image = UIImage(named: "myImage")!

    // define parameters
    let parameters = [
      "hometown": "yalikavak",
      "living": "istanbul"
    ]

    // Begin upload
    Alamofire.upload(.POST, "upload_url",
      // define your headers here
      headers: ["Authorization": "auth_token"],
      multipartFormData: { multipartFormData in

        // import image to request
        if let imageData = UIImageJPEGRepresentation(image, 1) {
          multipartFormData.appendBodyPart(data: imageData, name: "file", fileName: "myImage.png", mimeType: "image/png")
        }

        // import parameters
        for (key, value) in parameters {
          multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)
        }
      }, // you can customise Threshold if you wish. This is the alamofire's default value
      encodingMemoryThreshold: Manager.MultipartFormDataEncodingMemoryThreshold,
      encodingCompletion: { encodingResult in
        switch encodingResult {
        case .Success(let upload, _, _):
          upload.responseJSON { response in
            debugPrint(response)
          }
        case .Failure(let encodingError):
          print(encodingError)
        }
    })
  }

我知道我必须添加进度块,但不知道在哪里可以添加该进度块。

.progress { (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in
        println("\(totalBytesWritten) / \(totalBytesExpectedToWrite)")
    }

我想添加上面的块,如何计算视频上传的预计时间。

【问题讨论】:

    标签: ios swift alamofire


    【解决方案1】:

    你可以这样做:

    Alamofire.upload(
        .POST,
        URLString: "http://httpbin.org/post",
        multipartFormData: { multipartFormData in
            multipartFormData.appendBodyPart(fileURL: unicornImageURL, name: "unicorn")
            multipartFormData.appendBodyPart(fileURL: rainbowImageURL, name: "rainbow")
        },
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .Success(let upload, _, _):
                upload.progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
    
                let progress: Float = Float(totalBytesRead)/Float(totalBytesExpectedToRead) // you can give this progress to progressbar progress
    
                   let value = Int(progress * 100) // this is the percentage of the video uploading
    
    
                    print(totalBytesRead)
    
                }
                upload.responseJSON { request, response, result in
                    debugPrint(result)
                }
            case .Failure(let encodingError):
                print(encodingError)
            }
        }
    )
    

    【讨论】:

    • 如何在 uitableviewCell 中显示上传进度?
    【解决方案2】:

    您也可以在Alamofire中使用其他版本的upload方法并获得进度。

    let request = Alamofire.upload(.POST, "upload_url", headers: ["Authorization": "auth_token"], data: imageData!)
    
    
    request.responseJSON { (response: Response<AnyObject, NSError>) in
        switch response.result {
            case let .Success(result):
                // do something with data
                print(result)
            case let .Failure(error):
                // Error occurred
                print(error)
        }
    }
    
    request.progress { (bytesWritten: Int64, totalBytesWritten: Int64, totalExpectedBytesToWritten: Int64) in
    
        let uploadProgress = Float(totalBytesWritten) / Float(totalExpectedBytesToWritten)
        // Keep track of progress
    }
    

    【讨论】:

    • 能否将代码更新为 Alamofire 5 ? request.progress 不再可用:(
    猜你喜欢
    • 1970-01-01
    • 2014-12-17
    • 2014-07-28
    • 1970-01-01
    • 2018-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多