IONIC 3 - Я хочу получить путь к файлу PDF при выборе из файлового менеджера - PullRequest
0 голосов
/ 27 ноября 2018

Здесь я публикую свой код выбора PDF из файлового менеджера.Мне нужно, чтобы получить путь к файлу выбранного PDF.как я могу получить это?

.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 ]

0 голосов
/ 27 ноября 2018

Здесь вы можете найти ответ как для Android, так и для платформы iOS.Как выбрать pdf-документ и просмотреть этот документ.

.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));
 }
}

Надеюсь, это поможет вам.

0 голосов
/ 27 ноября 2018

лучшим подходом будет Document Viewer

Document Viewer

Этот плагин предлагает тонкий API для просмотра файлов PDF, которые либо хранятся в папке ресурсов приложения (/ www / *), либов любом другом каталоге файловой системы, доступном через плагин Cordova File.

$ 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)
0 голосов
/ 27 ноября 2018

, если вы выбираете из файлового менеджера, сделайте что-то подобное и откройте pdf из моего другого ответа

 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);
      });
0 голосов
/ 27 ноября 2018

Вы можете попробовать Ionic FileOpener

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