错误类型错误:无法读取 ionic4 中未定义的属性'download'



我正在尝试使用cordova插件文件传输下载pdf文件,但它捕获了以下错误 "无法读取未定义的属性'下载'">

app.module.ts imports:

import { File } from '@ionic-native/file/ngx';
import { FileTransfer } from '@ionic-native/file-transfer/ngx';

提供程序 [文件, 文件传输]

通过控制台.log我检查了 URL 是否正常工作,但下载方法不起作用

let path = null;
if (this.plateform.is('ios')) {
path = this.file.documentsDirectory;
} else {
path = this.file.dataDirectory;
}
this.fileTransfer.download(url, path + 'file.pdf').then( data => {
alert('download Complete');

用法:https://ionicframework.com/docs/native/file-transfer#usage

import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer/ngx';
import { File } from '@ionic-native/file';
constructor(private transfer: FileTransfer, private file: File) { }
...
const fileTransfer: FileTransferObject = this.transfer.create();
// Upload a file:
fileTransfer.upload(..).then(..).catch(..);
// Download a file:
fileTransfer.download(..).then(..).catch(..);
// Abort active transfer:
fileTransfer.abort();
// full example
upload() {
let options: FileUploadOptions = {
fileKey: 'file',
fileName: 'name.jpg',
headers: {}
.....
}
fileTransfer.upload('<file path>', '<api endpoint>', options)
.then((data) => {
// success
}, (err) => {
// error
})
}
download() {
const url = 'http://www.example.com/file.pdf';
fileTransfer.download(url, this.file.dataDirectory + 'file.pdf').then((entry) => {
console.log('download complete: ' + entry.toURL());
}, (error) => {
// handle error
});
}

您需要在使用前创建一个传输对象,即

const transfer: FileTransferObject = this.fileTransfer.create();
transfer.download(url, path + 'file.pdf').then( data => {
alert('download Complete');

最新更新