2016-08-11 59 views
1

如何獲得文件上傳的進度?使用Alamofire向文件上傳添加進度

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

我想添加上面的塊,如何計算視頻上傳的估計時間。

回答

2

,你可以這樣做:

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

如何在uitableviewCell中顯示上傳進度? –

0

您也可以使用Alamofire 上傳方法的其他版本,並且有進步。

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 
}