首页 > 解决方案 > 在 IONIC 3 中下载多个文件时出现错误代码 3(连接错误)

问题描述

我想从 ionic 3 的服务器下载多个文件(图像、pdf、视频和音频)。

使用离子文件传输插件:- https://ionicframework.com/docs/native/file-transfer/

我正在尝试从服务器下载大约 1k 个文件。但是当它启动时,它工作正常。但是在某些文件下载后,它会显示 FileTransferError 错误代码为 3,其余所有文件将显示相同的图像。

代码:-

// 此函数将从数据库中获取文件路径(1k)并将其传递给 fileDownloadFromServer 函数。

getDownloadFilePath() {
        let that = this;
            return new Promise((resolve, reject) => {
                that.database.executeSql('SELECT * FROM tableName').then((data) => {

                    for (let i = 0, len = data.rows.length; i < len; i++) {
                        that.fileDownloadFromServer(data.rows.item(i), data.rows.length).then((data) => { 
                        });
                    }
                }).catch(e => {
                    console.log("here hjere");
                    resolve("error");
                    console.log(e);
                })
            });
    }

// 这个函数实际上将使用文件传输对象从服务器下载文件。

fileDownloadFromServer(fileDownloadData, fileCount) {
     return new Promise((resolve, reject) => {
            let that = this;
            if (this.platform.is('ios')) {
                this.fileLocalpath = this.file.documentsDirectory;
            } else if (this.platform.is('android')) {
                this.fileLocalpath = this.file.dataDirectory;
            }


            that.options = {
                headers: {
                    "Authorization": "Basic " + that.authData
                }
            }

            let fileTransferObj: FileTransferObject = that.fileTransfer.create();

            let tmpFilePath = fileDownloadData.globalPath;
            let tmpFileName = fileDownloadData.folderName;

            let tmpRandomNumber = Math.floor(Math.random() * 10000000000000);
            let tmpSourceFilePath = encodeURI(downloadFilePath + tmpFilePath + '/' + tmpFileName);
            let tmpTargetFilePath = that.fileLocalpath + tmpRandomNumber + '_' + tmpFileName.replace(/ /g, "_");


            fileTransferObj.download(tmpSourceFilePath, tmpTargetFilePath, false, that.options).then((entry) => {
                console.log('download complete: ' + entry.toURL());
            }, (error) => {
                console.log("Error for taget file = "+tmpTargetFilePath)
                console.log(error);
            });
        });
    }

标签: angularionic3cordova-pluginsphonegap-pluginshybrid-mobile-app

解决方案


推荐阅读