0

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);
            });
        });
    }
Pratik Gavas
  • 119
  • 1
  • 8
  • Take a look at this [link](https://stackoverflow.com/questions/19275268/phonegap-file-transfer-of-picture-fails-on-every-other-picture-error-code-3-wit) – jeff chef Jul 19 '18 at 08:37
  • @jeffchef , I asked the question for downloading multiple files not for uploading. I have already tried to add option 'Connection' & 'chunkedMode', But it's not working. – Pratik Gavas Jul 19 '18 at 08:45

0 Answers0