Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to open (doc, ppt, xlsx, pdf, jpg ,png) file using ionic native file opener

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));
like image 488
Sandip Moradiya Avatar asked Feb 02 '18 13:02

Sandip Moradiya


1 Answers

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];
}
like image 198
Sandip Moradiya Avatar answered Sep 23 '22 01:09

Sandip Moradiya