我正在使用ionic 5电容器打开pdf文件,我将其作为blob接收。
downloadFile(data: Blob, fileName: string) {
this.blob = new Blob([data], { type: 'application/pdf' });
const reader = new FileReader;
reader.onload = () => {
console.log('3');
var base64data = reader.result as string;
// Save the PDF to the device
try {
Filesystem.writeFile({
path: fileName,
data: base64data,
directory: FilesystemDirectory.Data,
// encoding: FilesystemEncoding.UTF8
}).then(() => {
console.log('File Written');
Filesystem.getUri({
directory: FilesystemDirectory.Data,
path: fileName
}).then((getUriResult) => {
console.log('getUriResult', getUriResult.uri);
const path = getUriResult.uri;
this.platform.ready().then(() => {
this.fileOpener.open(path, 'application/pdf')
.then(() => console.log('File is opened'))
.catch(error => console.log('Error openening file', error));
})
}, (error) => {
console.log('getUri', error);
});
});
} catch (error) {
console.error('Unable to write file', error);
}
}
reader.readAsDataURL(this.blob);
}
我得到下面提到的错误,当我运行在真实的设备
Msg: Native: tried calling FileOpener.open, but the FileOpener plugin is not installed.
Msg: Install the FileOpener plugin: 'ionic cordova plugin add cordova-plugin-file-opener2'
Msg: Error openening file plugin_not_installed
我在网上搜索过同样的实现,但由于某种原因,它不起作用。
您需要安装cordova插件包和本地包:
npm install cordova-plugin-file-opener2
npm install @ionic-native/file-opener
npm install cordova-plugin-file
npm install @ionic-native/file
ionic cap sync