我有一个生成PDF的PHP服务。在我的方法中,我有以下代码,当我在浏览器中运行它时,它可以工作:
runReport(): void {
this.loading = true;
this.reportService.runReport(this.body)
.pipe(finalize(() => this.loading = false))
.subscribe(response => {
const file = new Blob([response.body], { type: 'application/pdf' });
const fileURL = URL.createObjectURL(file);
window.open(fileURL);
const a = document.createElement('a');
a.href = fileURL;
a.target = '_blank';
a.download = '-redacted filename-.pdf';
document.body.appendChild(a);
a.click();
});
}然而,当我使用电容器生成Xcode并将应用程序发布到我的手机上时,它不工作。我可以看到我的加载微调器在哪里出现,然后消失,但PDF从未下载。
有没有其他方法可以自动将文件下载到设备上?
发布于 2021-10-28 05:02:23
但是,当我使用电容器生成Xcode并将应用程序发布到我的手机上时,它不工作。
显然,这是行不通的,因为手机上没有window.open(fileURL);这样的东西。你需要做的就是在手机上下载你的pdf文件。然后使用手机自带的经典应用程序打开/查看下载的pdf文件。
const fileTransfer: FileTransferObject = this.transfer.create();
const url = "http://example.com/myfile.pdf";
fileTransfer.download(url, this.file.externalApplicationStorageDirectory + directory + "/" + "one.pdf", false, {
headers: {
Authorization: `Bearer ${token}`
}
}).then((entry) => {
console.log('download complete: ' + entry.toURL());
const options: DocumentViewerOptions = {
title: "PDF File",
};
this.previewAnyFile.preview(entry.toURL())
.then((res: any) => console.log(res))
.catch((error: any) => console.error(error));
}, (error) => {
console.log("Error: ", error);
});上面的代码将从给定的HTTP地址下载任何文件并将其保存在手机上。然后,previewAnyFile将使用当前安装的文档查看器来显示下载的pdf。
https://stackoverflow.com/questions/69747141
复制相似问题