Cordova video capture - how can I save file it out to server?

佐手、 提交于 2019-12-25 14:48:08

问题


I'm successfully capturing a video stream via Cordova Video Capture, and the file is saved to something like file:///storage/emulated/....

However I'm stuck on how to save the file out to a remote server by sending its source code (blob data?)

What I've tried (path is the path to the saved video.)

Attempt 1

This does return the file, but only as garbled text. If I set dataType: 'blob', the callback never fires.

Dom7.ajax({ //<-- I'm using Framework7, hence the Dom7 DOM/AJAX library
    url: path,
    success: function(file_content) { }
});

Attempt 2 - native AJAX - success callback never fires

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
    if (this.readyState == 4 && this.status == 200) {
        console.log('AJAX', typeof this.response);
    }
}
xhr.open('GET', path);
xhr.responseType = 'blob';
xhr.send();

Attempt 3 - via Cordova File plugin

window.resolveLocalFileSystemURL(
    cordova.file.applicationStorageDirectory,
    function(dir) {
        console.log('got app storage dir', dir); //<-- this fires
        dir.getFile(path, {create:false}, function(fileEntry) {
            console.log('got file!!!'); //<-- this doesn't
            fileEntry.file(function(file) {
                var reader = new FileReader();
                reader.onloadend = function(e) {
                    console.log(e);
                };
                reader.readAsDataURL(file);
            });
        });
    },
    function(err) {alert('didn\'t get it');
        console.log(err);
    }
);

...but since the file doesn't live in application storage (or its external application storage counterpart) this is academic.

This does return the file, but as text. How can I get it / convert it to a format I can send to a remote server over CORS?

To be clear, it's not the CORS part I'm asking about, just the getting of the video file content in a format suitable for sending.

[UPDATE]

window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, function(fs) {
    console.log(1, fs);
    fs.root.getFile(path, {create: false, exclusive: false}, function(entry) {
        console.log(2, entry);
    });
});

However only the first console.log fires, not the second.

来源:https://stackoverflow.com/questions/45716487/cordova-video-capture-how-can-i-save-file-it-out-to-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!