2016-04-26 67 views
1

我尝试使用Apache Cordova Plugin cordova-plugin-media-capture捕捉视频。我如何将此视频发送到我的服务器并存储它?捕获视频并将其存储在服务器上?

这是如何开始拍摄视频的官方例子:

// 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}); 

但我怎样才能将视频发送到我的服务器来存储呢? 我必须传递给我的ajax代码?

// 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 
      } 
     ); 
    } 
}; 
+0

将文件发送到服务器。可能需要先阅读文件... – dandavis

+0

这正是我的问题如何做到这一点... – Black

+0

请在下面检查我的答案。 –

回答

1

更好地使用这个函数。

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

赞下面。

uploadFile(mediaFiles[i]); 

编辑1

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

cordova-plugin-media 

cordova-plugin-media-capture 

cordova-plugin-file 

cordova-plugin-file-transfer 
+0

不幸的是我不能测试它,因为我总是得到'不能读取'未定义'的属性'capture' – Black

+0

然后可能是安装插件的问题。 –

+0

我安装了插件'cordova-plugin-camera'和'cordova-plugin-file' – Black

相关问题