Trouble uploading videos from iOS using WordPress REST api

I am very new at this, so sorry if I have missed something.

I am trying to upload a video from iOS to WordPress using the WP REST API and Alamofire.

Read More

Some videos will upload just fine while other videos get a rest_upload_no_content_disposition status = 400 error.

Here is my code to uploading a video

class func pageVideo(accessToken:String, filePath:NSURL, completion: AnyObject? -> Void) {

        let endpoint = "http://XXXXXXXXX.com/wp-json/wp/v2/media/?access_token=(accessToken)"

        let parameters = [
            "Content-Type": "multipart/form-data",
            "Content-Disposition": "attachment; filename=appVideo.mov",
            "media_type": "file"
        ]

        var fileData : NSData?
        if let fileContents = NSFileManager.defaultManager().contentsAtPath(filePath.path!) {
            fileData = fileContents
        }

        let mgr = Alamofire.Manager.sharedInstance
        mgr.upload(.POST, endpoint, multipartFormData: { multipartFormData in

            if let _fileData = fileData {
                multipartFormData.appendBodyPart(data: _fileData, name: "file", fileName: "file.mov", mimeType: "file/mov")
            }

            for (key, value) in parameters {
                multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)
            }

            }, encodingCompletion: { encodingResult in

                switch encodingResult {
                case .Success(let upload, _, _):
                    upload.response {(request, response, data, error ) in
                        do {
                            let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments)

                            if let dict = json as? NSDictionary{
                                if let url = dict.valueForKeyPath("source_url") as? String{


                                    completion(url)
                                }
                            }

                        } catch let error as NSError{
                            //                                completion(error.localizedDescription)
                            print(error.localizedDescription)
                        }


                    }

                case .Failure(let encodingError):
                    print(encodingError)
                }
        })

    }

}

It seems like shorter videos work while anything over 30 seconds fails.
Even shorter videos that do upload take a very long time.

Any help would be greatly appreciated.

Related posts

1 comment

  1. Nothing was wrong with my code I just needed to compress the videos. the longer videos were hitting the WordPress upload limit. Also it’s always a good idea to compress videos or images that your uploading somewhere.

Comments are closed.