Cordova File plugin readAsDataURL not returning file data

时光怂恿深爱的人放手 提交于 2019-12-24 06:08:07

问题


I am trying without success to use the readAsDataURL function of the Cordova File plugin to get a base64 version of a video file. My code looks like this:

  recordVideo()
  {
    return new Promise(resolve =>
    {
      let options: CaptureVideoOptions = { limit: 1, duration: 2 };
      MediaCapture.captureVideo(options)
        .then(
          (data: MediaFile[]) => {

            console.log('Media: recordVideo: cordova.file.dataDirectory = ' + cordova.file.dataDirectory + ', path = ' + data[0].fullPath.substring(1));

            // Turn the video file into base64
            let base64File = File.readAsDataURL(cordova.file.dataDirectory, data[0].fullPath.substring(1));

            console.log('Media: recordVideo: got video with data = ' + JSON.stringify(data));

            console.log('Media: recordVideo: base64File = ' + JSON.stringify(base64File));

            resolve(data);
          },
          (err: CaptureError) => console.error('ERROR - Media: recordVideo: captureVideo error = ' + err)
        );
    });
  }

The output from the first console.log shows the values of the parameters passed to the readAsDataURL:

Media: recordVideo: cordova.file.dataDirectory = file:///var/mobile/Containers/Data/Application/764345DC-A77D-43C2-9DF7-CDBE6A0DC372/Library/NoCloud/, path = private/var/mobile/Containers/Data/Application/764345DC-A77D-43C2-9DF7-CDBE6A0DC372/tmp/50713961066__4FD8AF8D-BD36-43A4-99CC-F328ADFD7E38.MOV

The second console.log shows the data returned by the MediaCapture plugin:

Media: recordVideo: got video with data = [{"name":"50713961066__4FD8AF8D-BD36-43A4-99CC-F328ADFD7E38.MOV","localURL":"cdvfile://localhost/temporary/50713961066__4FD8AF8D-BD36-43A4-99CC-F328ADFD7E38.MOV","type":"video/quicktime","lastModified":null,"lastModifiedDate":1485446813000,"size":195589,"start":0,"end":0,"fullPath":"/private/var/mobile/Containers/Data/Application/764345DC-A77D-43C2-9DF7-CDBE6A0DC372/tmp/50713961066__4FD8AF8D-BD36-43A4-99CC-F328ADFD7E38.MOV"}]

The last console.log shows the value returned by the readAsDataURL:

Media: recordVideo: base64File = {"__zone_symbol__state":null,"__zone_symbol__value":[]}

There is next to no documentation on using this (that I can find).


回答1:


Function readAsDataURL gets path and filename as parameters and returns a promise. The usage is

File.readAsDataURL("path_to_the_FileName", "Filename").then(result => {
  this.base64File = result;
});

As per the console log, the filename and full path to the filename are obtained from data (promise returned from MediaCapture.captureVideo).

So you can use it as below

var path = "file://"+data[0].fullPath.substring(7,data[0].fullPath.lastIndexOf("/"))‌​; 
File.readAsDataURL(path, data[0].name).then(result => { 
  this.base64File = result;
}



回答2:


If issue is this that File.readAsDataURL is not returning response nor catching error then move the cordova.js after vendor.js script (in the index.html). I was facing this issue in my ionic 3 app. I solved this from this link here.



来源:https://stackoverflow.com/questions/41878272/cordova-file-plugin-readasdataurl-not-returning-file-data

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