Conor Donohoe
Conor Donohoe

Reputation: 317

How do you use cordova file plugin in angular 8 without using Ionic framework

I am very much aware that there are posts out there that have something similar with a different plugin, but I haven't been able to find one to tell me first if I am bootstrapping my Cordova application to angular correctly and how do I tell, Second I want to save a JSON string or object if I can to the users phone

/* main.ts */
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { AppModule } from './app/app.module';
import { environment } from './environments/environment';

if (environment.production) {
  enableProdMode();
}
/* Bootstrap App Module */
const onDeviceReady = () => {
  platformBrowserDynamic().bootstrapModule(AppModule);
};
document.addEventListener('deviceready', onDeviceReady, false);
platformBrowserDynamic().bootstrapModule(AppModule)
  .catch(err => console.error(err));
  
  
/* Favourite Service Typescript file */
import { Injectable } from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import { NotificationService } from './notification.service';
import { EnvService } from './env.service';
import { catchError, tap } from 'rxjs/operators';
declare var window: any;
@Injectable({
  providedIn: 'root'
})
export class FaveService {
 createFav() {
    document.addEventListener('deviceready', function() {
      window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, success, error);
    }, false);
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) {

      console.log('file system open: ' + fs.name);
      fs.root.getFile('newPersistentFile.txt', { create: true, exclusive: false }, function (fileEntry) {

          console.log('fileEntry is file?' + fileEntry.isFile.toString());
          // fileEntry.name == 'someFile.txt'
          // fileEntry.fullPath == '/someFile.txt'
          writeFile(fileEntry, null);

      }, onErrorCreateFile);

  }, onErrorLoadFs);
  }
}

Above is the code I am using to try and interact with the Cordova file system API but I am getting these errors.

enter image description here

Now I know the errors will go away if I do declare const requestFileSystem but when I build the app and try to create a file, it only creates the directories in the data folder on the phones file system, not the file. I've also tried giving the app permissions to read/write access to storage and again no joy. so my other question is how do I get rid of these errors or am I doing the right thing by declaring them and make it work on android 7.0 thank you very much in advance

Upvotes: 0

Views: 1542

Answers (1)

Nidhin Joseph
Nidhin Joseph

Reputation: 10237

You are missing definitions of success and error in the code. Please refactor it as below.

document.addEventListener('deviceready', function() {
  window.(requestFileSystem as any)(LocalFileSystem.PERSISTENT, 0, success, error);
}, false);

function success(fs: any) {
  fs.root.getFile('newPersistentFile.txt', {
    create: true,
    exclusive: false
  }, function(fileEntry) {
    writeFile(fileEntry);
  }, error);
}

function error(e: any) {
  console.log('error', e);
}

function writeFile(fileEntry) {
  // logic to write to file
}

Upvotes: 0

Related Questions