i am developing Hybrid app using ionic. i want to open (doc, ppt, xlsx, pdf, jpg ,png) file from device internal or external storage using ionic native file opener plugin, but i'm able to open only pdf file using below code. i use application/pdf to open pdf, to open other files what shoud i replace at the place of application/pdf? please help me. thank you.
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));
finally i got solution.
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));
make other function for 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];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With