gpt4 book ai didi

ionic2 - Cordova 文件插件 readAsDataURL 不返回文件数据

转载 作者:行者123 更新时间:2023-12-04 01:48:50 24 4
gpt4 key购买 nike

我试图使用 Cordova 文件插件的 readAsDataURL 函数来获取视频文件的 base64 版本,但没有成功。我的代码如下所示:

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


第一个 console.log 的输出显示了传递给 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 插件返回的数据:
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 返回的值:
Media: recordVideo: base64File = {"__zone_symbol__state":null,"__zone_symbol__value":[]}

几乎没有关于使用它的文档(我可以找到)。

最佳答案

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

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

根据控制台日志,文件名和文件名的完整路径是从数据中获取的(从 MediaCapture.captureVideo 返回的 promise )。

所以你可以使用它如下
var path = "file://"+data[0].fullPath.substring(7,data[0].fullPath.lastIndexOf("/"))‌​; 
File.readAsDataURL(path, data[0].name).then(result => {
this.base64File = result;
}

关于ionic2 - Cordova 文件插件 readAsDataURL 不返回文件数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41878272/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com