2016-11-15 165 views
0

我想在iOS上將多個文件上傳到服務器。我有數據作爲NSData,我想上傳到服務器。我正在使用alamofire上傳數據。我已經通過他們的文件,但無法找到很好的答案。我無法理解下面的代碼如何使用NSData &多個圖像。請提供解決方案。如何使用alamofire上傳多個文件並顯示進度?

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

回答

4

使用以下代碼用於使用Alamofire進行多圖像上傳。你可以找到文檔周圍here: -

Alamofire.upload(
multipartFormData: { multipartFormData in 
    multipartFormData.append(data, name: "imageFile", 
       fileName: "image.jpg", mimeType: "image/jpeg"), 
    multipartFormData.append(data1, name: "image1File", 
       fileName: "image1.jpg", mimeType: "image/jpeg") 
}, 
to: "https://httpbin.org/post", //URL, 
headers: headers, 
encodingCompletion: { encodingResult in 
    switch encodingResult { 
    case .success(let upload, _, _): 
     upload.responseJSON { response in 
      debugPrint(response) 
     } 
     upload.uploadProgress(closure: { //Get Progress 
       progress in 
        print(progress.fractionCompleted) 
      }) 
    case .failure(let encodingError): 
     print(encodingError) 
    } 
}) 

注:這是使用Alamofre 4.0

+0

沒有您code.Though你,我已經改進的代碼錯誤。 – Techiee

+0

隨時編輯答案並加以改進。 @deepakkumar –

+0

如何在上傳過程中顯示uitableviewcell中的進度條? –

相關問題