IONIC 3 - I want to get file path of the pdf when

2019-05-30 07:27发布

问题:

Here i am posting my code of selecting PDF from file manager. i need to get file path of selected PDF. how can i get it?

.html

 <input type="file" (change)="selectPDF($event)" class="file-input upload-items" name='company_pdf' style="opacity: 0"
            id="company_pdf" #fileInp>

.ts

selectPDF(fileInput: any) {
if (fileInput.target.files[0].type == 'application/pdf') {
  console.log(fileInput.target.files)
  this.PDFfiles.push(fileInput.target.files[0]);
  if (this.PDFfiles.length > 4) {
    this.disablePdfUplaod = true;
  }
  //this.PDFfile = fileInput.target.files[0];
} else {
  this.shared.showToast('Please select PDF')
}
}

回答1:

You can try Ionic FileOpener

  this.fileOpener
        .open(filePath, fileExtension)
        .then(() => {
          return true
        });


回答2:

the best approach would be Document Viewer

Document Viewer

This plugin offers a slim API to view PDF files which are either stored in the apps assets folder (/www/*) or in any other file system directory available via the cordova file plugin.

$ ionic cordova plugin add cordova-plugin-document-viewer
$ npm install --save @ionic-native/document-viewer


import { DocumentViewer } from '@ionic-native/document-viewer';

constructor(private document: DocumentViewer) { }

const options: DocumentViewerOptions = {
  title: 'My PDF'
}

this.document.viewDocument('assets/myFile.pdf', 'application/pdf', options)


回答3:

if u are choosing from file manager do some like this and open the pdf from my other answer

 this.fileChooser.open()
      .then(
        uri => {
          this.filePath.resolveNativePath(uri)
            .then(file => {
              this.fileDir = file;
              this.fileName = file.substring(file.lastIndexOf("/") + 1);
             // open the pdf
             })
            .catch(err => console.log(err));
        }
      )
      .catch(error => {
        this.showError(error);
      });


回答4:

Here you can find both android and iOS platform answer. How to pick pdf document and view that document.

.html

<input  (click)="openFile()" class="file-input upload-items" name='company_pdf' style="opacity: 0"
            id="company_pdf" >

.ts

openFile() {
if (this.platform.is('android')) {
  this.fileChooser.open()
    .then(
      uri => {
        this.filePath.resolveNativePath(uri)
          .then(url => {
            console.log(url)
            // url is path of selected file
            var fileName = url.substring(url.lastIndexOf("/") + 1)
            console.log(fileName)
            // fileName is selected file name
          })
          .catch(err => console.log(err));
      }
    )
    .catch(error => {
      console.log(error)
    });
} else {
  this.docPicker.getFile('pdf')
  .then(uri => {
    console.log(uri)
    var fileName = uri.substring(uri.lastIndexOf("/") + 1)

  })
  .catch(e => console.log(e));
 }
}

Hope it will helps you.