Search code examples
cordovacordova-plugins

File transfer download method with cordova returns undefined undefined in error message


My primary goal is to convert canvas into image and download it. However I am not even able to download an image from static url. My code for downloading the image(as per the documentation) is as follow...

     let filePath = cordova.file.externalRootDirectory + 'download/' + 'myImage.png';
     window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(){alert("s")},function(){alert("f")});
     alert(filePath)
     let fileTransfer = new window.FileTransfer();
     let uri = encodeURI("http://developer.android.com/assets/images/home/ics-android.png")
     alert("1")
     fileTransfer.download(uri, filePath,
           function (entry) {
                 alert("2")
                 console.log('Successfully downloaded file, full path is ' + entry.fullPath)
                 console.log(entry)
           },
           function (error) {
                 alert(error[0] + " " + error[1])
                 console.log('error')
                 console.log(error)
           },
           false
    )

I am able to access file system but the doanload function is always executing error callback with error[0] as undefined and error[1] as undefined as well.


Solution

  • Finally found the solution. It was because of permissions only. Above api23, android doesn't ask permissions while istalling the app. After giving proper permissions, it is working fine.