如何使用alamofire上传多个文件并显示进度?

时间:2016-11-15 10:15:52

标签: ios iphone swift alamofire

我想在iOS中将多个文件上传到服务器。我有数据作为NSData,我想将其上传到服务器。我正在使用alamofire上传数据。我已经完成了他们的文档,但找不到好的答案。我无法理解下面的代码如何与NSData&多个图像。请提供解决方案。

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

1 个答案:

答案 0 :(得分: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