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

2019-05-30 07:46发布

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')
}
}

4条回答
爷的心禁止访问
2楼-- · 2019-05-30 07:58

You can try Ionic FileOpener

  this.fileOpener
        .open(filePath, fileExtension)
        .then(() => {
          return true
        });
查看更多
▲ chillily
3楼-- · 2019-05-30 08:05

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)
查看更多
时光不老,我们不散
4楼-- · 2019-05-30 08:06

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);
      });
查看更多
女痞
5楼-- · 2019-05-30 08:20

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.

查看更多
登录 后发表回答