Cordova文件插件readAsDataURL不返回文件数据 [英] Cordova File plugin readAsDataURL not returning file data

查看:147
本文介绍了Cordova文件插件readAsDataURL不返回文件数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Cordova File插件的readAsDataURL函数获取视频文件的base64版本,但没有成功。我的代码如下:

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

第一个控制台的输出.log 显示传递给 readAsDataURL 的参数的值:

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

第二个 console.log 显示MediaCapture插件返回的数据:

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"}]

最后一个 console.log 显示 readAsDataURL 返回的值:

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).

推荐答案

函数readAsDataURL获取路径和文件名作为参数并返回Promise。用法是

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

这篇关于Cordova文件插件readAsDataURL不返回文件数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆