Reputation: 8944
I want to upload multiple files to server in iOS. I have data as NSData and I want to upload it to server. I am using alamofire for uploading the data. I have gone through their documentation but could not find good answer. I am not able to understand how will the code below work with NSData & multiple images. Please provide solution.
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)
}
Upvotes: 0
Views: 3971
Reputation: 2143
For Multiple image upload with progress ios swift
Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(data, withName: "file", fileName: "image.jpg", mimeType:"image/jpeg")
multipartFormData.append(data1, withName: "file", fileName: "image1.jpg", mimeType:"image/jpeg")
},
to: url, method: .post, headers: headers) { (result) in
switch result{
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
let progressVal = String(format: "%.2f%%", progress.fractionCompleted * 100)
print("\(progressVal)")
})
upload.responseJSON{ response in
print(response)
}
case .failure(let error):
print("Error in upload: \(error.localizedDescription)")
}
}
Upvotes: 0
Reputation: 303
Use the following code for multiple image uploads using Alamofire. You can find documentation around it 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)
}
})
Note: This is using Alamofre 4.0
Upvotes: 4