# FileOpener and DocumentViewer are not working

**URL:** <https://forum.ionicframework.com/t/fileopener-and-documentviewer-are-not-working/169300>\
**Category:** Ionic Native\
**Created:** [July 22, 2019, 7:42am UTC](https://forum.ionicframework.com/t/fileopener-and-documentviewer-are-not-working/169300 "2019-07-22T07:42:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![OliverPrimo](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/oliverprimo/32/121442_2.png) [@OliverPrimo](https://forum.ionicframework.com/u/OliverPrimo)\
**Post date:** [July 22, 2019, 7:42am UTC](https://forum.ionicframework.com/t/fileopener-and-documentviewer-are-not-working/169300/1 "2019-07-22T07:42:25Z")

</div>

Hello Everyone,

I created a function that will get the PDF Blob file from the server and save it to phone’s storage. Now, I want the user to be able to open it once the file was downloaded. I tried using FileOpener (cordova-plugin-file-opener2) but it didn’t work without showing any errors. Same with trying the DocumentViewer (cordova-plugin-document-viewer) but it also didn’t work without showing any errors. I am testing the app on an actual mobile phone and tried to alert the error if there’s any since the moblie phone have no consoles but it doesn’t display any errors. I don’t know how to make it work or if there is anything that I can use aside from FileOpener and DocumentViewer. Btw, here are my codes.

**FileOpener**

```auto
              this.fileOpener.open(filePath, 'application/pdf').then(response => {
                this.toastSrvc.presentToast(response);
              })
              .catch(error => {
                this.alertCtrl.create({
                  title: 'Error opening the file',
                  message: error,
                  buttons: [
                    {
                      text: 'Close',
                      role: 'cancel',
                      handler: () => {
                        console.log('Alert Closed!');
                      }
                    }
                  ]
                }).present();
              });

```

**DocumentViewer**

```auto
this.documentViewer.viewDocument(filePath, 'application/pdf', options, this.onShow, this.onClose, this.onMissingApp, this.onError);

  onShow() {
    this.alertCtrl.create({
      message: 'Show'
    }).present();
  }

  onError() {
    this.alertCtrl.create({
      message: 'Error'
    }).present();
  }

  onMissingApp() {
    this.alertCtrl.create({
      message: 'Missing App'
    }).present();
  }

  onClose(){
    this.alertCtrl.create({
      message: 'Close'
    }).present();
  }

```

I hope someone can help me with this. Thank you in advance 🙂

---

<div class="post-metadata">

**Author:** ![liamod94](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/liamod94/32/127162_2.png) [@liamod94](https://forum.ionicframework.com/u/liamod94)\
**Post date:** [July 22, 2019, 9:12am UTC](https://forum.ionicframework.com/t/fileopener-and-documentviewer-are-not-working/169300/2 "2019-07-22T09:12:28Z")

</div>

I had to consume the pdf as a blob and save it to the device, before I could open it (rather than opening it directly from the url). I used File.writeFile(), which will return the document.

---

<div class="post-metadata">

**Author:** ![OliverPrimo](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/oliverprimo/32/121442_2.png) [@OliverPrimo](https://forum.ionicframework.com/u/OliverPrimo)\
**Post date:** [July 22, 2019, 11:06am UTC](https://forum.ionicframework.com/t/fileopener-and-documentviewer-are-not-working/169300/3 "2019-07-22T11:06:20Z")

</div>

Thank you for your hint but I already tried doing that if you mean it this way

```auto
    this.file.writeFile(filePath, fileName, pdfFile, {replace: true}).then(fileEntry: FileEntry => {
      let alert = this.alertCtrl.create({
        title: 'Export',
        message: 'file successfully saved!',
        buttons: [
          {
            text: 'Open File',
            handler: () => {
                this.fileOpener.open(fileEntry, 'application/pdf').then(response => {
                  this.toastSrvc.presentToast(response);
                })
                .catch(error => {
                  this.alertCtrl.create({
                    title: 'Error opening the file',
                   message: error,
                   buttons: [
                     {
                       text: 'Close',
                       role: 'cancel',
                       handler: () => {
                         console.log('Alert Closed!');
                       }
                     }
                   ]
                 }).present();
              });
            }
          }
        ]
      });
      alert.present();
    });

```

Or maybe I am missing something. I really don’t know what to do. Been stuck here for a while now. I am open your suggestion. Thanks again 🙂

---

<div class="post-metadata">

**Author:** ![liamod94](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/liamod94/32/127162_2.png) [@liamod94](https://forum.ionicframework.com/u/liamod94)\
**Post date:** [July 22, 2019, 2:35pm UTC](https://forum.ionicframework.com/t/fileopener-and-documentviewer-are-not-working/169300/4 "2019-07-22T14:35:45Z")

</div>

I do something like this:

```auto
let fileName = 'myFile.pdf';
let documentDirectory = this.platform.is('android') ? this.file.dataDirectory : this.file.documentsDirectory;
this.httpClient.get(url, headers).then(data => {
    let blob = new Blob([data], {type: 'application/pdf'});
    this.file.writeFile(documentDirectory, fileName, data).then(data => {
       resolve(documentDirectory + filename);
});
//Now you can do all your file opening stuff

```

That is how I got it to work at least anyway!

---

<div class="post-metadata">

**Author:** ![OliverPrimo](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/oliverprimo/32/121442_2.png) [@OliverPrimo](https://forum.ionicframework.com/u/OliverPrimo)\
**Post date:** [July 23, 2019, 4:21am UTC](https://forum.ionicframework.com/t/fileopener-and-documentviewer-are-not-working/169300/5 "2019-07-23T04:21:02Z")

</div>

I can get the PDF Blob File from the server and save it to the phone storage successfully, my only problem is opening the downloaded file.

---

<div class="post-metadata">

**Author:** ![ahmidbbc](https://avatars.discourse-cdn.com/v4/letter/a/8e8cbc/32.png) [@ahmidbbc](https://forum.ionicframework.com/u/ahmidbbc)\
**Post date:** [April 1, 2020, 5:31pm UTC](https://forum.ionicframework.com/t/fileopener-and-documentviewer-are-not-working/169300/6 "2020-04-01T17:31:30Z")

</div>

Hey,

Try this to access ‘www/assets’ for Android device:

```auto
let filePath = this.file.applicationDirectory + 'public/assets/';

```
