Ionic 5 - File API is not working - file.readAsArrayBuffer always returns null

Viewed 684

I'm trying to read files (images, PDFs, etc) but the Ionic Native File API is not working as expected. Follow the code:

import { Entry, FileEntry, File } from '@ionic-native/file/ngx';
import { FilePath } from '@ionic-native/file-path/ngx';
import { FileChooser } from '@ionic-native/file-chooser/ngx';

export class DocumentCreationPage extends implements OnInit, OnDestroy {
  constructor(
        private androidPermissions: AndroidPermissions,
        private fileChooser: FileChooser,
        private filePath: FilePath,
        private file: File
      ) {}

    async openFiles() {
        this.fileChooser
        .open()
        .then(contentPath => {
          const that = this;
          this.filePath.resolveNativePath(contentPath).then(uri => {
        
            const lastSeparator = uri.lastIndexOf('/');
            const fileName = uri.substr(lastSeparator + 1);
            const sysPath = uri.substr(0, lastSeparator);

            that.file.readAsArrayBuffer(sysPath, fileName).then(buff => {
                // buff is always null
                const blob = new Blob([new Uint8Array(buff)], { type: 'image/jpg' });
            });
          });
        });
    }
}

I can open the file chooser component, select the file, and get its name, size, and full location, but I cannot read the file content. I don't know if there is any additional step or configuration needed to solve this problem.

0 Answers
Related