4

I am using phonegap to record a video and I am wanting to save the base64 data-encoded string. So far I have tried this..

function captureSuccess(mediaFiles) {
    var i, path, len;
    path = mediaFiles[0];
    win(path);
}

function win(file) {
    var reader = new FileReader();
    reader.onloadend = function (evt) {
        console.log("read success");
        console.log(evt.target.result);
    };
    reader.readAsDataURL(file); 
};

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

function captureVideo() {
    navigator.device.capture.captureVideo(captureSuccess, captureError, {limit: 1});
}

I have used readAsDataURL as specified in the documentation. The output of evt.target.result is "data:video/mp4;base64," but there isn't any encoded data after the filetype.

Is there anything else I need to add in order to get the full base64 data of the video?

I am really struggling to find anything that can help me. Any help would be greatly appreciated.

Pooshonk
  • 1,284
  • 2
  • 22
  • 49

1 Answers1

1
var b64toBlobAlt = function(dataURI, contentType) {
  var ab, byteString, i, ia;
  byteString = atob(dataURI.split(',')[1]);
  ab = new ArrayBuffer(byteString.length);
  ia = new Uint8Array(ab);
  i = 0;
  while (i < byteString.length) {
    ia[i] = byteString.charCodeAt(i);
    i++;
  }
  return new Blob([ab], {
    type: contentType
  });
};
var path = mediaFiles[0].fullPath;

window.resolveLocalFileSystemURL(path, function(fileEntry) {
  return fileEntry.file(function(data) {
    var reader = new FileReader();
    reader.onloadend = function(e) {
      var blob = b64toBlobAlt(e.target.result, 'video/mp4');
      if (blob) {
         // do whatever you want with blob
        });
      }
    };
    return reader.readAsDataURL(data);
  });
});
Matt
  • 74,352
  • 26
  • 153
  • 180
kevohagan
  • 75
  • 1
  • 6
  • 1
    Hi kevohagan, please consider adding an explanation as to how your code works. – Matt Jul 29 '15 at 20:00
  • @kevohagan my code stuck in `reader.readAsDataURL(data)`. It doesn't giving any error but not coming in `onloadend` function. – Vivek Jain Mar 24 '21 at 19:22
  • @VivekJain It is likely that you received an error. You can listen for errors using `reader.onerror = function (error) {}` – Leon W Oct 21 '22 at 06:05