Reputation: 1215
I tried to select file using UIImagePicker. But when I tried to upload file using Alamofire
multipartFormData it gives following error
"multipartEncodingFailed(Alamofire.AFError.MultipartEncodingFailureReason.bodyPartURLInvalid(assets-library://asset/asset.MOV?id=00178364-C308-4D2F-9B06-ECFBF21B9128&ext=MOV))"
Method use to get file url
func selectVideoFromLibrary(sender: Any) {
let imagePickerController = UIImagePickerController()
imagePickerController.sourceType = .photoLibrary
imagePickerController.delegate = self
imagePickerController.mediaTypes = ["public.movie"]
present(imagePickerController, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
videoURL = info["UIImagePickerControllerReferenceURL"] as? NSURL
self.dismiss(animated: true, completion: nil)
}
Alamofire
upload to upload file to server
func uploadVideo(){
let serverURL = AppConfig.BASEPATH + AppConfig.UPLOAD
print("upload link is",AppConfig.BASEPATH + AppConfig.UPLOAD)
let vURL = self.videoURL! as URL
let vName = videoName.data(using: .ascii)
let pName = name.data(using: .ascii)
let pNo = phoneNo.data(using: .ascii)
Alamofire.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(vURL, withName: "uploadedfile")
multipartFormData.append(vName!, withName: "title")
multipartFormData.append(pName!, withName: "pname")
multipartFormData.append(pNo!, withName: "phoneno")
},
to: serverURL,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
print(response.result.value)
}
case .failure(let encodingError):
print(encodingError)
}
})
}
Upvotes: 1
Views: 1264
Reputation: 61
using info[UIImagePickerControllerMediaURL] to get file url(file://...) instead of the assets-library path will sovle your problem.
as the following code
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
videoURL = info[UIImagePickerControllerMediaURL] as? NSURL
self.dismiss(animated: true, completion: nil)
}
Upvotes: 1