该插件工作正常,直到最近我开始遇到它的问题,代码不执行也不抛出错误。我想从相机捕获图像并将 base64
字符串发送到服务器,这很容易,因为我可以直接获取捕获图像的 base64
但后来我使用了 native crop它返回裁剪图像的 URI
。所以现在我必须得到这个图像的 base64
,但是 Base64
插件不再工作了。非常感谢任何解决方法或帮助。
我使用的代码:
this.base64.encodeFile(filePath).then((base64File: string) => {
console.log(base64File); // Won't execute
}, (err) => {
console.log(err); // Won't execute
});
最佳答案
我在同一个错误上花了几个小时。碰巧这个插件刚刚停止工作(它仍然是测试版)。我对代码进行了一些更改,并用 File Plugin 替换了 Base64 插件。
- 将文件插件添加到 app.module.ts 导入。
- 在组件/服务构造函数中导入和注入(inject)依赖项。
如果你有文件路径:
// split file path to directory and file name
let fileName = filePath.split('/').pop();
let path = filePath.substring(0, filePath.lastIndexOf("/") + 1);
this.file.readAsDataURL(path, fileName)
.then(base64File => {
console.log("here is encoded image ", base64File)
})
.catch(() => {
console.log('Error reading file');
})
关于cordova - Base64 插件不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48638248/