【问题标题】:File Transfer Plugin ionic 2文件传输插件离子 2
【发布时间】:2017-03-11 10:43:28
【问题描述】:

单击按钮时,我正在尝试下载歌曲。

这是我的 .ts 文件。

download() {
    this.platform.ready().then(()=>{
    const fileTransfer = new Transfer();
    let url = 'http://think-digital.in/wp-content/uploads/2017/03/varla-varla-vaa.mp3';
    fileTransfer.download(url, cordova.file.dataDirectory + 'file.mp3').then((entry) => {
      console.log('download complete: ' + entry);
      console.log('download complete: ' + entry.toURL());
    }, (error) => {
      // handle error
      console.log(error);
    });
  });
  }

我遇到这样的错误

TypeError: 无法读取未定义的属性“文件”

我做错了什么

【问题讨论】:

标签: angular ionic2 cordova-plugins file-transfer


【解决方案1】:

我认为错误是因为这一行:... cordova.file.dataDirectory ...,由于未定义cordova,您无法从未定义访问file 属性。尝试将其添加到文件顶部:declare var cordova: any;

请确保您仅在移动设备上运行该部分代码,因为在浏览器中运行应用时无法使用cordova

this.platform.ready().then(() => {
    if (this.platform.is('cordova')) {
        ... your code 
    }
});

【讨论】:

  • 是的,我在给出这个错误之后就这样做了,我在@component上方声明了我的汽车cordova
  • 在物理设备上运行应用程序时是否遇到该错误?
  • 您是否在等待平台准备好后再执行该代码?
  • 是的,你可以看到我的问题我已经给出了平台读取功能
  • 我没有尝试过使用 if condition wait 之后我会告诉你
猜你喜欢
  • 1970-01-01
  • 2021-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-30
  • 1970-01-01
  • 1970-01-01
  • 2013-01-18
相关资源
最近更新 更多