sgrebnov / cordova-plugin-background-download

Apache License 2.0
73 stars 77 forks source link

Empty file for multiple downloads #9

Closed enricodeleo closed 6 years ago

enricodeleo commented 9 years ago

Hi!

I'm trying to get multiple files in iOS, I have something like this:

//save multiple files within the same directory, event is fired after directory creation
$scope.$on( "createDir", function() {
      toDownload.forEach(function( value, index ) { //iterate through an array of urls

        var fileName = URI(value).filename(), //extracting the filename from the original file
        uriString = value; // the url value from the current array index

        // open target file for download
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {

        fileSystem.root.getFile( directoryName + '/' + fileName, { create: true }, function (targetFile) {
            var downloader = new BackgroundTransfer.BackgroundDownloader();
            // Create a new download operation.
            var download = downloader.createDownload( uriString, targetFile );
            // Start the download and persist the promise to be able to cancel the download.
            promises.push( download.startAsync() ); //added to an array since we'll use angular's $q.all(promises).then [...]
          });

        });

      });

      $q.all(promises).then(function(res) {
         // etc...
      });
    });

Checking the downloaded files it seems that only the last file is been written and the others are 0 bytes. I tried to live check the directory and I noticed that the last file changes size up and down during download, so I'm pretty sure that the download of all the files works but they are written sequentially on the same file (the last one).

Any hint? Thank you in advance! :)

ps what about porting https://github.com/thibaultCha/TCBlobDownload ?

--- UPDATE --- I solved for now by downloading a zip and unzipping onSuccess. I was wondering if I can notify the user somehow when the download ends and the app is in background mode. Ideas?

arunap commented 9 years ago

Hello, I'm sure you can use local push notification plugin to update user within onSuccess function.

NickToropov commented 6 years ago

Hi @enricodeleo! we've recently added possibility to run concurrent downloads for iOS. Please try it! I'll close this issue but feel free to reopen it or report a new issue if you have any questions.

raubreak commented 7 months ago

Hello! I'm trying to download concurrent zipped files in IOS but doesn't works, in Android works well.

For example: At the first time, I'm lunching the download A, when this download haves 10% completed, lunch the second download B. This download B is starting directly at 10% and the download B is stopped, both downloads become a corrupted.

Seems like the promises combined in only one.

my code is very simple, i use VUE to manage the promises and stop the downloads when user wants:

`

             window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) { fileSystem.root.getFile(fileName, { create: true }, function (targetFile) {
                  (async () => {

                      var uriMatcher = "^" + fileName.replace(".", "\\.") + "$";

                      const downloader = new BackgroundTransfer.BackgroundDownloader(uriMatcher);

                      const download = downloader.createDownload(uriString, targetFile,chapter.title);

                      const onSuccess = function () { 
                          const fileSaved ={
                              dirPath:targetFile.nativeURL,
                              fileName:fileName,
                          }
                          delete self.descargasActivas[chapter.id];
                          resolve(fileSaved);
                      }

                      const onError = function (error) {
                          console.log('Download startAsync Error: ', error);
                          delete self.descargasActivas[chapter.id];
                          reject(error);
                      }
                      const onProgress = function (progressEvent) { // progress callback    
                          const progress = Math.round(progressEvent.bytesReceived / progressEvent.totalBytesToReceive * 100);
                          // console.log(progress,'%' ,chapter.id );
                          chapter.downloadProgress = progress;
                      }

                      // Start the download and persist the promise to be able to cancel the download.
                      const downloadPromise = download.startAsync().then(onSuccess, onError, onProgress);
                      await f7.store.dispatch('addDownloadPromise',{id:chapter.id, promise: downloadPromise});

                  })();

              },(error) => {
                  console.error('Error al obtener el archivo:', error);
                  reject(error);
              });

`

Any solution? I'm trying to pass a uriMatcher in BackgroundDownloader but doesn't works.