【问题标题】:How to download a file with ionic 5如何使用 ionic 5 下载文件
【发布时间】:2020-10-29 01:46:27
【问题描述】:

我正在尝试使用我的 ionic 应用程序下载一个文件,但它不起作用,该文件是一个视频文件,我想在下载时显示一个进度条,它的工作原理是当用户点击下载时,它会检查目录AppName是否存在,它不会创建目录,然后下载目录中的文件。

这是我的代码

download() {
    this.chkmkDir().then(async ()=>{
        const fileTransfer: FileTransferObject = this.fileTransfer.create();
        this.storageDirectory = `${this.file.dataDirectory}${api.AppName}/`;
        fileTransfer.onProgress((progressEvent)=>this.progress=Math.round((progressEvent.loaded / progressEvent.total) * 100))
        let fileName=await this.service.getFileName('mp4')
        var URL = encodeURI('test.mp4');
        fileTransfer.download(URL,`${this.storageDirectory}${fileName}`, true)
    })
}
chkmkDir(){
    return new Promise((resolve, reject)=>{
        this.file.checkDir(this.file.dataDirectory, api.AppName).then(res=>resolve()
        ).catch(err => {
            this.file.createDir(this.file.dataDirectory, api.AppName,false).then(res =>resolve())
            .catch(err=>reject(JSON.stringify(err))); 
        });
    })
} 

请问我做错了什么,我在控制台中收到此错误 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'then' of undefined TypeError: Cannot read property 'then' of undefined

【问题讨论】:

    标签: ionic-framework


    【解决方案1】:

    您正在使用的插件不存在。您的代码无法从插件中读取类。这可能是由于插件不兼容或与插件相关的其他依赖问题。

    当您没有正确设置或配置插件时,也会发生这种情况。请检查您的 app.module.ts 文件。并检查控制台中是否记录了与插件版本不兼容相关的任何错误。简而言之,您所有的插件都应该与您的 "@ionic-native/core" 版本具有相同的版本。

    例如:

    package.json

    "@ionic-native/core": "^5.0.0",
    "@ionic-native/diagnostic": "^5.0.0",
    "@ionic-native/dialogs": "^5.0.0",
    "@ionic-native/file": "^5.0.0",
    "@ionic-native/file-path": "^5.0.0",
    "@ionic-native/file-transfer": "^5.0.0",
    "@ionic-native/http": "^5.0.0",
    

    【讨论】:

    • 我试过了,但没用。问题在于这个函数this.file.checkDir(), this.file.createDir() 没有将其视为一个承诺,我添加到它的 then 方法是什么给了我错误ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'then' of undefined TypeError: Cannot read property 'then' of undefined
    • this.file.createDir 来自调用原生插件的插件库。如果设置不正确,它将是未定义的。
    • 在您的控制台中尝试以下代码:“ionic cordova platform rm android”,然后“ionic cordova platform add android”。它将重新安装您的 Cordova 插件
    • 我也试过了,但还是出现同样的错误信息
    • 有编译错误吗?尝试使用“ionic cordova run android --device --verbose”
    猜你喜欢
    • 2019-01-27
    • 2020-09-05
    • 2021-07-15
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    • 2017-09-29
    • 2018-11-18
    相关资源
    最近更新 更多