-1

I need to save dataURL pdf file in phone's directory in Android using Cordova with ngCordova.My code is Given below:-

var dataURL="data:application/pdf;base64,JVBERi0xLjMKJf////8KNiAwIG9iago8PAovVHlwZSAvRX...";
$cordovaFile.createFile(cordova.file.externalRootDirectory,dataURL, true)
          .then(function (success) {
             alert('success');
          }, function (error) {
            alert('Fails');
          });

It not working.I need to save PDF in dataURL format using Cordova Android.How to do it.

Sabir
  • 235
  • 4
  • 15

2 Answers2

2

Check this code

  var dataURL="data:application/pdf;base64,JVBERi0xLjMKJf////8KNiAwIG9iago8PAovVHlwZSAvRX...";
  $cordovaFile.writeFile(cordova.file.externalRootDirectory,'filename',dataURL, true)
                      .then(function (success) {
                         alert('Download Completed. Check your Internal/External Storage..');
                      }, function (error) {
                        alert('Fails');
                      });
Shahid Neermunda
  • 1,317
  • 1
  • 14
  • 28
  • This method did not work for me. The file was not encoded correctly and had to use the [solution here](https://stackoverflow.com/questions/4998908/convert-data-uri-to-file-then-append-to-formdata) – OJ7 Nov 14 '18 at 18:30
1

Please read documentation

createFile(path, file, replace) function only creates new file without writing it.

You need to use writeFile(path, file, data, replace)

dlanberg
  • 9
  • 1