2

I want to write a text file in Ionic and I've tried many different methods and neither of them works

import { Filesystem, Directory, Encoding } from '@capacitor/filesystem';
writeSecretFile = async () => {
    await Filesystem.writeFile({
      path: 'secrets/text.txt',
      data: "This is a test",
      directory: Directory.Documents,
      encoding: Encoding.UTF8,
    });
  };

In this case, My app wont even start, source

import { FileOpener } from '@ionic-native/file-opener/ngx';
  fun() {
    this.fileOpener
      .open('./filer.pdf', 'application/pdf')
      .then(() => console.log('File is opened'))
      .catch(e => console.log('Error opening file', e));
  }
}

this wont work either Stackbuiltz code All the efforts are highly appreciated

Sadaf Shafi
  • 1,016
  • 11
  • 27
  • this wasn't helpful either https://stackoverflow.com/questions/33643107/read-and-write-a-text-file-in-typescript#:~:text=import%20%7B%20readFileSync%20%7D%20from%20'fs,a%20class%20without%20keyword%20const%20. – Sadaf Shafi Jun 08 '21 at 05:52
  • 1
    Please add more details. Its a little vague saying it just doesn't work – misha130 Jun 08 '21 at 06:22
  • 1
    iOS or Android? – WSD Jun 08 '21 at 06:24
  • @misha130 I gave the link to stackbuiltz where the code is written please take a look, and as a result the app wont even start, no error messages no nothing at all :(( – Sadaf Shafi Jun 08 '21 at 06:29
  • @guzmanoj for now testing on the browser but later on for android – Sadaf Shafi Jun 08 '21 at 06:29
  • 1
    do you have imported the package in the `app.module.ts`? – RSD Jun 08 '21 at 08:23
  • @SadafShafi it won't work on your browser. – WSD Jun 08 '21 at 09:22
  • 1
    @guzmanoj According to the docs it does work in PWAs, I assume they're using IndexedDB. – Thomas Jun 08 '21 at 16:57
  • @Thomas genuine question... where? https://capacitorjs.com/docs/apis/filesystem – WSD Jun 08 '21 at 17:57
  • 1
    @RSD thanks a lot I just did that and it worked, would you please write it as an answer so that I'd accept it and close the issue – Sadaf Shafi Jun 09 '21 at 09:45
  • @guzmanoj Apparently I was looking at the v2 docs which has icons. But there is a web version in the repo: https://github.com/ionic-team/capacitor-plugins/blob/main/filesystem/src/web.ts – Thomas Jun 09 '21 at 11:18

1 Answers1

2

You also have to import the package into your app.module.ts

RSD
  • 178
  • 8