首页 文章

文件传输插件离子2

提问于
浏览
1

我点击按钮时试图下载一首歌 .

这是我的.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:无法读取未定义的属性“文件”

我在做什么错

1 回答

  • 0

    我认为错误是因为这一行: ... cordova.file.dataDirectory ... ,因为未定义cordova,所以无法从undefined访问 file 属性 . 尝试在文件顶部添加: declare var cordova: any;

    并且请确保只有当它是移动设备时才运行该部分代码,因为在浏览器中运行应用程序时无法使用cordova

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

相关问题