I want to download multiple files(images, pdf, video and audio) from server in ionic 3.
Using Ionic File Transfer Plugin :- https://ionicframework.com/docs/native/file-transfer/
I am trying to download around 1k files from server. But when it starts, it works fine. But after some files are downloading, It shows an error with FileTransferError with Error Code 3 and rest of all the files will display the same image.
CODE:-
// This function will get path of files(1k) from database and pass it to fileDownloadFromServer function .
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); }) }); }
// This function will actually download file from server using filetransfer object.
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); }); }); }