在角度/离子应用程序中,我有以下代码从互联网上下载pdf。
public pdfDownload(){
const fileTransfer: FileTransferObject = this.transfer.create();
const mime = 'application/pdf';
const pdfFile = 'http://www.pdf995.com/samples/pdf.pdf';
// alert(this.file.dataDirectory);
fileTransfer.download(pdfFile, this.file.dataDirectory + 'file.pdf', true)
.then((entry) => {
alert('download complete: ' + entry.toURL());
}, (error) => {
// handle error
});
}
答案 0 :(得分:1)
在第二个参数中使用 this.file.externalDataDirectory +'file.pdf'而不是 this.file.dataDirectory +'file.pdf' 函数fileTransfer.download(1个参数,2个参数,3个参数);
e.g。
fileTransfer.download(pdfFile,this.file.externalDataDirectory +'file.pdf',true)
答案 1 :(得分:0)
async downloadFile() {
await this.fileTransfer.download("https://cdn.pixabay.com/photo/2017/01/06/23/21/soap-bubble-1959327_960_720.jpg", this.file.externalRootDirectory +
'/Download/' + "soap-bubble-1959327_960_720.jpg");
}
getPermission() {
this.androidPermissions.hasPermission(this.androidPermissions.PERMISSION.WRITE_EXTERNAL_STORAGE)
.then(status => {
if (status.hasPermission) {
this.downloadFile(); // your file transfer method
}
else {
this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.WRITE_EXTERNAL_STORAGE)
.then(status => {
if(status.hasPermission) {
this.downloadFile(); // your file transfer method
}
});
}
});
}
重要说明:
其中许多使用READ_EXTERNAL_STORAGE
,因此它不会存储您的文件。您应该尝试使用WRITE_EXTERNAL_STORAGE
的权限。
100%可以在离子4 ^上工作。