首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误下载:无法读取ionic4中未定义的属性‘TypeError’

错误下载:无法读取ionic4中未定义的属性‘TypeError’
EN

Stack Overflow用户
提问于 2019-08-25 12:49:39
回答 2查看 911关注 0票数 0

我正在尝试使用cordova-plugin- file -transfer下载pdf文件,但它捕获到以下错误"Cannot read property ' download‘of undefined“

app.module.ts导入:

代码语言:javascript
复制
import { File } from '@ionic-native/file/ngx';
import { FileTransfer } from '@ionic-native/file-transfer/ngx';

提供程序文件,FileTransfer

通过console.log,我检查到url工作正常,但下载方法不工作

代码语言:javascript
复制
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');
EN

回答 2

Stack Overflow用户

发布于 2019-08-25 15:18:24

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

代码语言:javascript
复制
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
  });
}
票数 0
EN

Stack Overflow用户

发布于 2019-08-25 17:59:41

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

代码语言:javascript
复制
const transfer: FileTransferObject = this.fileTransfer.create();
代码语言:javascript
复制
transfer.download(url, path + 'file.pdf').then( data => {
 alert('download Complete');
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57643323

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档