1

I am trying to upload a video to a server file through Alamofire but I couldn't get the "data" going to be passed..its always nil

    var videoURL = NSURL(string: "")
   //returns Optional(file:///private/var/mobile/Containers/Data/Application/1FB40086-228B-4011-A9D4-7874E2EEF9F4/tmp/4A6AAD76-B899-4B67-8E96-925DA4AE9E93.mov)


    let videodata =  NSData(contentsOfFile: (videoURL?.absoluteString)!)
    //nil  
    let url = NSURL(fileURLWithPath: (videoURL?.absoluteString)!)

    let videodata = NSData(contentsOf: url as URL)
    //nil

If I get data would lead a way for me to do this:

 Alamofire.upload(multipartFormData: { multipartFormData in
                multipartFormData.append (videodata as! Data, withName: "file", fileName: "file.mov", mimeType: "video/quicktime")
enter code here

EDIT::

thank you guys, with your help I have struggled my way out of there to this file not found error, but I can see the file is being saved in my gallery, any clue would save my day.

      print (videoURL!)
//returns file:///private/var/mobile/Containers/Data/Application/3F280477-DA16-4A67-AE60-D6247143352E/tmp/1E4AC002-6AD0-41E1-9E0D-A09B697F81F7.mov


       print (videoURL!.path!)
      // returns /private/var/mobile/Containers/Data/Application/3F280477-DA16-4A67-AE60-D6247143352E/tmp/1E4AC002-6AD0-41E1-9E0D-A09B697F81F7.mov


        var videoData = NSData()

        let path = videoURL!.path!
        if FileManager.default.fileExists(atPath: path) {

        }else {
               print("Could not fin file at url: \(videoURL!.path!)")
             // here it throws file not found
        }
Code Tree
  • 1,822
  • 3
  • 21
  • 33
  • `.absoluteString` is *not* the right method to convert a URL to a path, compare http://stackoverflow.com/questions/34135305/nsfilemanager-defaultmanager-fileexistsatpath-returns-false-instead-of-true. – Martin R Dec 08 '16 at 14:45
  • yeah kind of tried everything... just with videoURL but went it vain – Code Tree Dec 08 '16 at 14:48
  • 1
    videoUrl is Optional in your code so you need to unwrap it just placing ! at end of videoURL! – Sanju Dec 08 '16 at 15:02
  • did that :| still the same – Code Tree Dec 08 '16 at 15:07
  • 1
    So, right after you declare `var videoURL` and hit any of the lines below, when you print it, it shows `Optional(file:///private/var/mobile/Containers/Data/Application/1FB40086-228B-4011-A9D4-7874E2EEF9F4/tmp/4A6AAD76-B899-4B67-8E96-925DA4AE9E93.mov` ? When you option click videoURL, what type of object does it show that it is? An optional URL? An optional String? – Kayla Galway Dec 08 '16 at 15:25

2 Answers2

2

In Swift 3 use native URL and Data instead of NSURL and NSData.

if let videoURL = URL(string: urlString), let videodata = try? Data(contentsOf: videoURL) {
     //Add code of Alamofire here
}
Nirav D
  • 65,660
  • 11
  • 144
  • 172
1

Using absoluteString returns a string that includes file:// at the beginning and you don't want that. You need to return the path of the URL

guard let videoPathString = videoURL.path as? String else {
  //handle error here if you can't create a path string
  return
}

var videoData = NSData()

//check if file exists at this path first 
if (NSFileManager.defaultManager().fileExistsAtPath(videoPathString)) {
  videoData = NSData(contentsOfFile: NSString(videoPathString))
} else {
  //if file does not exist at that path, handle here
}
Kayla Galway
  • 642
  • 4
  • 7