Black
Black

Reputation: 20312

Capture video and store it on the server?

I try to capture a video with Apache Cordova Plugin cordova-plugin-media-capture. How can i send this video to my server and store it?

This is an official example on how to start capturing a video:

// capture callback
var captureSuccess = function(mediaFiles) {
    var i, path, len;
    for (i = 0, len = mediaFiles.length; i < len; i += 1) {
        path = mediaFiles[i].fullPath;
        // do something interesting with the file
    }
};

// capture error callback
var captureError = function(error) {
    navigator.notification.alert('Error code: ' + error.code, null, 'Capture Error');
};

// start video capture
navigator.device.capture.captureVideo(captureSuccess, captureError, {limit:2, duration: 10});

But how can i send the video to my server to store it? What do i have to pass to my ajax code?

// capture callback
var captureSuccess = function(mediaFiles) 
{
    var i, path, len;
    for (i = 0, len = mediaFiles.length; i < len; i += 1) 
    {
        path = mediaFiles[i].fullPath;
        $.ajax
        (
            "ajax.php", 
            {
                type: "POST",
                data: { path: path }   //This will just send the path to the server
            }
        );
    }
};

Upvotes: 1

Views: 521

Answers (1)

Jay Rathod
Jay Rathod

Reputation: 11245

Better to use this Function.

function uploadFile(mediaFile) {
    var ft = new FileTransfer(),
        path = mediaFile.fullPath,
        name = mediaFile.name;
    var options = new FileUploadOptions();
    options.mimeType = "video/mpeg";
    options.fileName = name;
    options.chunkedMode = true;

    ft.upload(path,
        "**Your WebService Url Goes Here**",
        function(result) {
            console.log('Upload success: ' + result.responseCode);
            console.log(result.bytesSent + ' bytes sent');
        },
        function(error) {
            console.log('Error uploading file ' + path + ': ' + error.code);
        },
        options);
}

Call this function where you are getting your File Path.

Like Below.

uploadFile(mediaFiles[i]);

EDIT 1

Note: Make sure you added all below plugins in your project.

cordova-plugin-media

cordova-plugin-media-capture 

cordova-plugin-file

cordova-plugin-file-transfer 

Upvotes: 1

Related Questions