Save Response(Data Stream) as a file in device (iOS/Android) storage in cordova

I’m developing cordova(with ionic framework) mobile application for both iOS and Android platforms and I need to get download file into device and open it in default applications.(ex: download pdf and open it using pdf viewer).

so, here I use two plugins(cordova cordovaFileTransfer, cordova cordovaFileOpener2 ) for download file and open that file in default application. using those plugins I manage to download file(file with url) and open it. but need to give file URL to cordovaFileTransfer plugin.

In my situation file data came as a response(http.POST) and response is a data stream…

“%PDF-1.5↵%����↵1 0 obj↵<…”

here is my existing code(use url of pdf to download)

` var url = “https://www.tutorialspoint.com/angularjs/angularjs_tutorial.pdf”;
var targetPath = cordova.file.externalRootDirectory + “downloadFile.pdf”;

    $cordovaFileTransfer.download(url, targetPath, {}, true).then(function (result) {
        console.log('Success');
        $cordovaFileOpener2.open(
            '/sdcard/tokenApp/angularjs_tutorial.pdf', // Any system location, you CAN'T use your appliaction assets folder
            'application/pdf'
        ).then(function() {
            console.log('Success');
            $ionicLoading.hide();
        }, function(err) {
            console.log('An error occurred: ' + JSON.stringify(err));
        });

    }, function (error) {
        console.log('Error');
    }, function (progress) {
        // PROGRESS HANDLING GOES HERE
          console.log('Downloading..');

           });

    });

using above code I can not download file (because file came as a data stream).How can I download file using that stream…??

after that I tried to create blob object and then open it in InAppBrowser. but that give me a blank window. this is code for that…

  var file = new Blob([response], {type: 'application/pdf'});  
  $scope.fileURL = URL.createObjectURL(file);
  window.open($scope.fileURL, '_blank'); `

I would really appreciate any idea/bunch of code/whatever that helps me with this. Thank you…

I found solution. I used $cordovaFile.writeFile method to create new file

Hi @uditha_prasad

I am facing same issue and googling got to the same solution, using $cordovaFile.writeFile.

But I am not able to see the final file in iOS. I got the success call callback to be invoked, but I dont know where the file is saved. Using cordova.file.documentsDirectory as path.

What i am missing?

thanks in advance!