Getting wrong file name for pdf or other type files except image file using input in Ionic(Android)?


#1

I am using html input for selecting a file from file manager in my Ionic3/Angular application for Android/iOS. This below code is perfectly working in iOS and Browser. But, causing problem in Android for pdf files. I am using below code for this:

// in .html file

<input #fileUpload type="file" name="myfile"(change)="onFileChoose($event)"/>

// in .ts file

  onFileChoose($event): void {
    this.fileChooser.getFileInfo($event).then((result) => {
      this.fileName = result.fileName;
      this.fileData = this.sanitizeFileData(result.fileData);
      this.fileSize = result.fileSize;
      this.fileType = result.fileType;
    }, (error) => {
      this.helperProvider.createAlert('Alert', 'File is corrupted.');
    });
  }

 getFileInfo(event: Event): Promise<any> {

    let target = event && event.target;
    let files: Array<File> = target && target['files'];

    console.log(files[0].type);
    console.log(files[0].name);

    return new Promise((resolve, reject) => {
      if (files && files.length) {
        files = Array.from(files);
        let fileName = files[0].name;
        let fileSize = files[0].size;
        let fileType = files[0].type;
        let fileReader = new FileReader();

        fileReader.onload = () => resolve({
          fileData: fileReader.result,
          fileName: fileName,
          fileSize: fileSize,
          fileType: fileType
        });

        fileReader.onerror = error => reject(error);

        fileReader.onabort = error => reject(error);

        fileReader.readAsDataURL(files[0])
      }
    });
  }

I could perfectly select an image file from file manager in android and get the type, size and name of the file. But, when i select a pdf, suppose from downloads folder of file manager, i am getting the file type as empty and the file name as a different name than the original pdf file name. But, the size of the pdf file is correct.

Suppose, my pdf file name is “abc.pdf” but when i select the pdf file, i got the name as 5002 a random number and the type of the file as empty. I also saw that i am getting this behavior in all the files except the image files.

NOTE:

This is perfectly working in browser and iOS. But it is showing this weird behavior in android device.

Should i need any permission for reading pdf files using file manager in android?

Can anyone help me on this?


#2

If you want to select file for native app use File Opener Plugin


#3

Can you point me out what am i missing for pdf files while using input?

FileOpener plugin needs the path to the file. But, how will i know beforehand which file am i choosing and what file type of the file is?