0

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.

Mitya
  • 33,629
  • 9
  • 60
  • 107
  • Try window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, function(fs){ ... to load your video file. See https://stackoverflow.com/questions/30041230/accessing-android-internal-storage-with-cordova-file-plugin – Eric Aug 16 '17 at 14:40
  • Continued thanks, @Eric. See update. – Mitya Aug 16 '17 at 14:46
  • Perhaps the problem is `fs.root` takes me up to a certain point, but then I'm giving it an absolute URL prefixed with `file:///`? – Mitya Aug 16 '17 at 14:47
  • The first `console.log` says the directory I've just opened (`fs`) is `"file:///data/user/0/com.phonegap.helloworld/cache/"`. However my file lives in `file:///storage`. – Mitya Aug 16 '17 at 14:49
  • Perhaps Cordova's FileTransfer API is what I need... https://stackoverflow.com/questions/26154810/how-to-get-image-object-using-cordova – Mitya Aug 16 '17 at 14:55
  • Please give me full URL where your video file is stored – Eric Aug 16 '17 at 14:57
  • @Eric - example: file:///storage/emulated/0/DCIM/Camera/VID_20170817_113107.3gp – Mitya Aug 17 '17 at 10:27
  • In your attempt with Cordova File plugin, what is the value of the path variable??? – Eric Aug 17 '17 at 12:43
  • Literally what I gave you in my last comment. That is the filepath MediaCapture gives me after saving the file. In any case, I've actually just solved my problem, at long last. Thanks for your help, though. https://stackoverflow.com/questions/45734955/cordova-filetransfer-sends-file-but-nothing-received/45735265#45735265 – Mitya Aug 17 '17 at 13:02

0 Answers0