Using FileChooser and FilePath to read text File


#1

In my app, i want user to choose file in memory, read content of this file as text and save it. So iam using FileChooser and FilePath plugin. Problem is that function for reading file in File plugin takes two parametres: directory path and file name. But from Filepath plugin i get only whole adress of the file. Are there any convinient functions to get path of directory and file name separately? Or do i have to go with splitting the whole string and then reconstruct the path from its parts?

  openFile(){
    this.fileChooser.open().then(uri=>{
      console.log(uri);
      this.filePath.resolveNativePath(uri).then((path)=>{
        console.log(path);
        this.file.readAsText(path,'testFile.txt').then((data)=>{
          console.log(data);
        }).catch(err=>console.log(err));
      })
    });
  }

#2

Hi, this here is working for me:

this.fileChooser.open().then(file => {
    this.filePath.resolveNativePath(file).then(resolvedFilePath => {

      let path = resolvedFilePath.substring(0, resolvedFilePath.lastIndexOf('/'));
      let file = resolvedFilePath.substring(resolvedFilePath.lastIndexOf('/')+1, resolvedFilePath.length);
      this.readCsvData(path, file)
    
    }).catch(err => {
      alert(JSON.stringify(err));
    });

  }).catch(err => {
    alert(JSON.stringify(err));
});

and then:

private readCsvData(path, file) {
  
  this.file.readAsBinaryString(path, file)
  .then(content=>{
    console.log("File-Content: "+ JSON.stringify(content));
    this.extractData(content)
  })
  .catch(err=>{
    console.log(err);
    alert(JSON.stringify(err));
  });

}