Uploading video using URL swift Alamofire
Solution 1
You are trying to upload video by URL, that is not possible, in multipartFormData
need data to upload not URL, so firstly convert it to Data
then upload it.
Function for show imagePickerController only for Video's:
func showImagePicker(){
let picker = UIImagePickerController()
picker.delegate = self
picker.mediaTypes = [kUTTypeMovie as String]
self.present(picker, animated: true, completion: nil)
}
UIImagePickerControllerDelegate
function, that work after select video:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
picker.dismiss(animated: true, completion: nil)
guard let videoUrl = info[UIImagePickerControllerMediaURL] as? URL else {
return
}
do {
let data = try Data(contentsOf: videoUrl, options: .mappedIfSafe)
print(data)
// here you can see data bytes of selected video, this data object is upload to server by multipartFormData upload
} catch {
}
}
Solution 2
With Alamofire 5 , you can do this to upload a video to server:
import Alamofire
func uploadVideo(videoUrl: URL) { // local video file path..
let timestamp = NSDate().timeIntervalSince1970 // just for some random name.
AF.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(videoUrl, withName: "image", fileName: "\(timestamp).mp4", mimeType: "\(timestamp)/mp4")
}, to: endPoint! ).responseJSON { (response) in
debugPrint(response)
}
}
Note: endPoint is a string . Example: http://172.10.3.7:5000/uploadvideo
Related videos on Youtube
Muneeb Rehman
Updated on June 04, 2022Comments
-
Muneeb Rehman over 1 year
I need to upload a video to server using alamofire. The user selects the video and I get URL in didFinishPickingMediaWithInfo successfully as follows:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { picker.dismiss(animated: true, completion: nil) if let pickedVideo = info[UIImagePickerControllerMediaURL] as? URL { print(pickedVideo) } }
And then I upload the video using the following code:
Alamofire.upload( multipartFormData: { multipartFormData in multipartFormData.append(videoUrl, withName: "video", fileName: "video.mp4", mimeType: "video/mp4") }, to: url, encodingCompletion: { encodingResult in switch encodingResult { case .success(let upload, _, _): upload.responseJSON { response in if let JSON = response.result.value as? NSDictionary { completion(true) } else { completion(false) print(response) } } case .failure(let encodingError): print(encodingError) completion(false) } })
It enters in the failure block, and the following error displays:
multipartEncodingFailed(Alamofire.AFError.MultipartEncodingFailureReason.bodyPartFileNotReachableWithError(atURL: file:/private/var/mobile/Containers/Data/Application/C662AB0E-6A4F-40FB-9949-7F0A5AA2BA49/tmp/52C86F07-5DCC-413A-9F8C-71BBF33F793C.MOV -- file:///, error: Error Domain=NSCocoaErrorDomain Code=260 "The file “52C86F07-5DCC-413A-9F8C-71BBF33F793C.MOV” couldn’t be opened because there is no such file.
-
Rocky over 5 yearsVideo can't upload via URL, convert it to
Data
object and then upload it. -
staticVoidMan over 5 yearsshow us the code where you create your
videoUrl
-
-
Muneeb Rehman over 5 yearsthe message displaying is "The file “2C6D0E52-482B-4D8E-B3B7-F8B9CDF3E858.MOV” couldn’t be opened because there is no such file."
-
Muneeb Rehman over 5 yearsYes now i am converting my url to data object.
-
Muneeb Rehman over 5 yearsWhen converting url to data, the data is nil. I think url is not being generated properly
-
Muneeb Rehman over 5 yearsData object is empty that is why I am unable to upload it.
-
Muneeb Rehman over 5 yearsI think url is not correct, that is why program is unable to convert url into data object, hence returning data object as nil. I am getting such a url: file:///private/var/mobile/Containers/Data/Application/B5BF6C95-3708-4A94-9485-0AE01F0AFF31/tmp/C81EE7B5-B8BA-4915-8851-FCD33B93703A.MOV
-
Rocky over 5 yearswhat is output of
print(data)
statement? -
Muneeb Rehman over 5 yearsIssue was at my end. Thanks @Rocky for your help.
-
Raghav Chopra about 5 years@MuneebRehman What was the issue. ? I am facing the same problem
-
user28434'mstep over 4 yearsWhy
as NSString as String
?