San*_*iya 5 file ionic3 angular
我正在使用离子技术开发混合应用程序。我想使用离子本机文件打开工具插件从设备内部或外部存储打开(doc,ppt,xlsx,pdf,jpg,png)文件,但我只能使用以下代码打开pdf文件。我使用application / pdf打开pdf,打开其他文件我应该在application / pdf处替换什么?请帮我。谢谢。
import { FileOpener } from '@ionic-native/file-opener';
constructor(private fileOpener: FileOpener) { }
...
this.fileOpener.open('path/to/file.pdf', 'application/pdf')
.then(() => console.log('File is opened'))
.catch(e => console.log('Error openening file', e));
Run Code Online (Sandbox Code Playgroud)
终于我得到了解决方案。
let fileExtn=file_name.split('.').reverse()[0];
let fileMIMEType=this.getMIMEtype(fileExtn);
this.fileOpener.open("file:///storage/emulated/0/download/"+ file_name+"", fileMIMEType)
.then(() => console.log('File is opened'))
.catch(e => console.log('Error openening file', e));
Run Code Online (Sandbox Code Playgroud)
为MIMEtype设置其他功能
getMIMEtype(extn){
let ext=extn.toLowerCase();
let MIMETypes={
'txt' :'text/plain',
'docx':'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'doc' : 'application/msword',
'pdf' : 'application/pdf',
'jpg' : 'image/jpeg',
'bmp' : 'image/bmp',
'png' : 'image/png',
'xls' : 'application/vnd.ms-excel',
'xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'rtf' : 'application/rtf',
'ppt' : 'application/vnd.ms-powerpoint',
'pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
}
return MIMETypes[ext];
}
Run Code Online (Sandbox Code Playgroud)