2016-12-31 43 views
0

我試圖通過Alamofire 4.0將視頻上傳到服務器,並且我想添加進度條以顯示上傳過程中上傳過程的百分比,我如何通過Alamofire上傳的相同功能來做到這一點。如何在Alamofire 4.0中添加帶有標籤進度條的上傳進度百分比

我的上傳功能的代碼:

Alamofire.upload(multipartFormData: { multipartFormData in 



         multipartFormData.append(url.absoluteURL!, withName: "videoFile", fileName: "alaa", mimeType: "mov") 
         multipartFormData.append("video".data(using: .utf8)!, withName: "load") 
         multipartFormData.append("record".data(using: .utf8)!, withName: "type") 


        }, with: URL, encodingCompletion: { (result) in 
         // code 
         print("uploaded") 
        }) 

回答

1

要直接從AlamoFire文檔引用:

上傳進度

當你的用戶正在等待他們上傳來完成,有時 可以方便地顯示上傳到用戶的進度。任何 UploadRequest都可以使用uploadProgress和downloadProgress API報告上傳進度和 響應數據的下載進度。

let fileURL = Bundle.main.url(forResource: "video", withExtension: "mov") 

Alamofire.upload(fileURL, to: "https://httpbin.org/post") 
    .uploadProgress { progress in // main queue by default 
     print("Upload Progress: \(progress.fractionCompleted)") 
    } 
    .downloadProgress { progress in // main queue by default 
     print("Download Progress: \(progress.fractionCompleted)") 
    } 
    .responseJSON { response in 
     debugPrint(response) 
    } 
+0

其中是該行中視頻的URL? let fileURL = Bundle.main.url(forResource:「video」,withExtension:「mov」) –

+0

它意味着獲取名爲video.mov的文件在您的主包中的URL。 – bubuxu

+0

這是示例代碼,而不是特定於您問題的代碼。該行爲應用程序包創建名爲「video.mov」的文件的文件url。 –

0

請檢查一下。

  var imgData = Data() 
      if image != nil { 

       imgData = UIImageJPEGRepresentation(image!, 1.0)! 
      } 

      Alamofire.upload(multipartFormData:{ multipartFormData in 
       multipartFormData.append(imgData, withName: "image", fileName: "imagefilename", mimeType: "image/jpeg") 

       for (key, value) in param { 

        //let data = (value as! String).data(using: String.Encoding.utf8)! 
        let data = (value as AnyObject).data(using: String.Encoding.utf8.rawValue) 
        multipartFormData.append(data!, withName: key as! String) 
       } 
      }, 
          usingThreshold:UInt64.init(), 
          to:fullLink, 
          method:.post, 
          headers:[:], 
          encodingCompletion: { encodingResult in 

           switch encodingResult { 

           case .success(let upload, _, _): 

            upload.uploadProgress { progress in // main queue by default 

             print("Upload Progress: \(progress.fractionCompleted)") 
            } 

            upload.responseJSON { response in 

             debugPrint(response) 

             if let TempresponseDict:NSDictionary = response.result.value as? NSDictionary { 

              if (TempresponseDict.object(forKey: "response") as? String)?.caseInsensitiveCompare("success") == .orderedSame { 

               CompletionHandler(true, TempresponseDict) 
              } 
              else { 

               var statusCode = response.response?.statusCode 

               if let error = response.result.error as? AFError { 

                statusCode = error._code // statusCode private 

                switch error { 

                case .invalidURL(let url): 
                 print("Invalid URL: \(url) - \(error.localizedDescription)") 
                case .parameterEncodingFailed(let reason): 
                 print("Parameter encoding failed: \(error.localizedDescription)") 
                 print("Failure Reason: \(reason)") 
                case .multipartEncodingFailed(let reason): 
                 print("Multipart encoding failed: \(error.localizedDescription)") 
                 print("Failure Reason: \(reason)") 
                case .responseValidationFailed(let reason): 
                 print("Response validation failed: \(error.localizedDescription)") 
                 print("Failure Reason: \(reason)") 

                 switch reason { 

                 case .dataFileNil, .dataFileReadFailed: 
                  print("Downloaded file could not be read") 
                 case .missingContentType(let acceptableContentTypes): 
                  print("Content Type Missing: \(acceptableContentTypes)") 
                 case .unacceptableContentType(let acceptableContentTypes, let responseContentType): 
                  print("Response content type: \(responseContentType) was unacceptable: \(acceptableContentTypes)") 
                 case .unacceptableStatusCode(let code): 
                  print("Response status code was unacceptable: \(code)") 
                  statusCode = code 
                 } 
                case .responseSerializationFailed(let reason): 

                 print("Response serialization failed: \(error.localizedDescription)") 
                 print("Failure Reason: \(reason)") 
                 // statusCode = 3840 ???? maybe.. 
                } 

                print("Underlying error: \(error.underlyingError)") 
               } 
               else if let error = response.result.error as? URLError { 

                print("URLError occurred: \(error)") 
               } 
               else { 

                print("Unknown error: \(response.result.error)") 
               } 

               print("\(statusCode)") // the status code 

               CompletionHandler(false, TempresponseDict) 
              } 
             } 
             else { 

              CompletionHandler(false, NSDictionary()) 
             } 
            } 

           case .failure(let encodingError): 

            print(encodingError) 
            CompletionHandler(false, NSDictionary()) 
           } 
      }) 
     } 
+0

fullLink我必須把它作爲字符串或urlrequest,因爲在第二種情況下,它給我null,所以視頻不上傳 –

+0

,這部分我不明白,我如何處理,如果我有我的代碼2參數以上 。 // let data =(value as!String).data(using:String.Encoding.utf8)! } –

+0

fullLink = String –