I'm trying to upload video to my server. I select video from album but "fatal error: unexpectedly found nil while unwrapping an Optional value".
Please find the below code for more information.
let videoURL: String = NSBundle.mainBundle().pathForResource("IMG_2859", ofType: "MOV")!
// var videoData: NSData = NSData.dataWithContentsOfURL(NSURL.fileURLWithPath(videoURL))!
print(videoURL)
let data = NSData(contentsOfFile: videoURL)
print(data)
let session = NSURLSession.sharedSession()
let request = NSMutableURLRequest()
print(session)
print(request)
request.URL = NSURL(string: "uploadvideo/uploadtoserver?user_email=\(candit_email)")
print(request.URL)
request.HTTPMethod = "POST"
let boundary = "------------------------8744f963ff229392"
request.addValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
let postData = NSMutableData()
postData.appendData("--\(boundary)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
print("Upload Video File1")
postData.appendData("Content-Disposition: form-data; name=\"filedata\"; filename=\"MOV\"\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
print("Upload Video File2")
postData.appendData("Content-Type: video/x-msvideo\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
print("Upload Video File3")
postData.appendData(NSData(data: data!))
print("Upload Video File4")
postData.appendData("\r\n--\(boundary)--\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
print("Upload Video File5")
request.HTTPBody = postData
print("Upload Video File6")
print(request.HTTPBody)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler:{ (response: NSURLResponse?, data: NSData?, error: NSError?) -> Void in
let error: AutoreleasingUnsafeMutablePointer<NSError?> = nil
do{
if let jsonResult: NSDictionary! = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers) as? NSDictionary{
}
} catch let error as NSError {
print(error.localizedDescription)
}
})
}
I had referred this below two links but no use for me.
1) Imageor-video-posting-to-server-in-swift
2) how-to-upload-video-to-server-from-iphone