2016-11-15 163 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中的进度条? –

相关问题