IONIC 3 - 我想从文件管理器中获取 select 时的 pdf 文件路径
IONIC 3 - I want to get file path of the pdf when select from file manager
我在这里发布了从文件管理器中选择 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')
}
}
你可以试试Ionic FileOpener
this.fileOpener
.open(filePath, fileExtension)
.then(() => {
return true
});
最好的方法是文档查看器
文档查看器
此插件提供了一个纤薄的 API 来查看 PDF 文件,这些文件要么存储在应用程序资产文件夹 (/www/*) 中,要么存储在通过 cordova 文件插件可用的任何其他文件系统目录中。
$ 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)
如果您从文件管理器中选择,请执行类似操作并从我的其他答案中打开 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);
});
在这里您可以找到 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));
}
}
希望对您有所帮助。
我在这里发布了从文件管理器中选择 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')
}
}
你可以试试Ionic FileOpener
this.fileOpener
.open(filePath, fileExtension)
.then(() => {
return true
});
最好的方法是文档查看器
文档查看器
此插件提供了一个纤薄的 API 来查看 PDF 文件,这些文件要么存储在应用程序资产文件夹 (/www/*) 中,要么存储在通过 cordova 文件插件可用的任何其他文件系统目录中。
$ 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)
如果您从文件管理器中选择,请执行类似操作并从我的其他答案中打开 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);
});
在这里您可以找到 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));
}
}
希望对您有所帮助。