我有一个使用InAppBrowser开发的Ionic 4应用程序。实际上,该应用几乎完全是通过InAppBrowser完成的:
启动画面>传递给InAppBrowser的URL>网站加载。 用户登录,单击链接并浏览站点。 用户可能单击的某些链接为PDFS(内容类型= application / pdf),但文件名末尾没有.pdf扩展名。
在iOS中,这些加载正常。在Android中,PDF永远不会显示。...
我已阅读这是已知的解决方法。我无法使用Google Docs查看器嵌入解决方法,因为pdf文件的URL是基于我们网站上已通过身份验证的网站来查看和访问PDF的。
我正在阅读的另一个选项是获取URL,将文件下载到用户的本地系统浏览器;然后将其打开-所有这些都使用FileSystem,File,FileTransfer和FileOpener。以下是我的代码块和Android Studio LOGCAT的输出:
downloadPDF(pUrl: string) {
console.log("==== in downloadOpenPDF function ====");
if(this.platform.is('android')) {
var permissions = cordova.plugins.permissions;
permissions.hasPermission(permissions.WRITE_EXTERNAL_STORAGE,function( status ){
if ( status.hasPermission ) {
console.log("We DO have access to write to external storage");
}
else {
console.warn("We DO NOT have access to write to external storage");
permissions.requestPermission(permissions.WRITE_EXTERNAL_STORAGE,this.success,this.error);
}
});
permissions.hasPermission(permissions.READ_EXTERNAL_STORAGE,function( status ){
if ( status.hasPermission ) {
console.log("We DO have access to read to external storage");
}
else {
console.warn("We DO NOT have access to read to external storage");
permissions.requestPermission(permissions.READ_EXTERNAL_STORAGE,this.error);
}
});
}
let downloadUrl = pUrl;
console.log("downloadUrl is ================"+downloadUrl);
let path = this.file.dataDirectory;
let filepath = path+'mcfrenderPDF.pdf';
console.log("path is =================="+path);
const transfer: FileTransferObject = this.fileTransfer.create();
transfer.download(downloadUrl,filepath,true).then(entry => {
let url = entry.toUrl();
console.log("url is ================="+url);
if(this.platform.is('ios') || this.platform.is('android')) {
console.log("================ Use Document Viewer");
this.document.viewDocument(url,'application/pdf',{});
} else {
//console.log("Device is Android = Use Google Docs");
//this.openGoogleDocsPDF(pUrl);
console.log("local path to file is: "+path+'mcfrenderPDF.podf');
this.fileOpener.open(entry.URL,'application/pdf');
}
})
}
我的日志输出如下:
2019-11-03 13:26:05.401 5213-5333/com.cat.fpd.mobile.mcf D/FileTransfer: Saved file: file:///data/user/0/com.cat.fpd.mobile.mcf/files/mcfrenderPDF.pdf
2019-11-03 13:26:05.401 5213-5333/com.cat.fpd.mobile.mcf E/FileTransfer: File plugin cannot represent download path