How to write data to a file using ngCordova?

I write here the same question because anyone answered to me…

Hi guys,
I’ve a trouble with download of a PDF file created with pdfmake. I have created and viewed the pdf made from charts.js, but now I would like to download the file.

I tried to use ngCordova:

var blob = new Blob([pdf], {type: ‘application/pdf’});

$scope.pdfUrl = URL.createObjectURL(blob);

$cordovaFile.writeFile(cordova.file.dataDirectory,‘fileName.pdf’,$scope.pdfUrl, true).then(function(result){
alert(‘Success! Survey created!’);
}, function(err) {
console.log(“ERROR”);
})

But I got an error 1: NOT_FOUND_ERR

What’s wrong? Please help me…I’m going crazy!! Thank you all guys

Hey , Did you fix your app ? I’m having this problem exactly right now … Thank you

Try to read this one https://forum.ionicframework.com/t/problem-with-angular-pdf-and-pdfmake/50140/11

Let me know if it solves your problem.

Hi All,

I used writeFile(path, file, data, replace) method in $cordovaFile to write new file(pdf, jpg, text ).
as a data I got data stream from restful service …(it looks like %PDF-1.5↵%����↵1 0 obj↵…)
The problem is I can write a file but it opens as a blank pdf but file size is correct.
for data field I used data stream itself, blob object(var blob = new Blob([data], { type: ‘application/pdf’ }):wink: ,

This is my code

   `     $cordovaFile.writeFile(cordova.file.externalRootDirectory, "myCreatedPdf.pdf", blob, true)
        .then(function(success){
           console.log("file");
        `$cordovaFileOpener2.open(
                success.target.localURL,'application/pdf'
            );
        },function (error) {
            console.log("error");
        })`

Please help me out… any ideas…?

Hi All,

I used writeFile(path, file, data, replace) method in $cordovaFile to write new file(pdf, jpg, text ).
as a data I got data stream from restful service …(it looks like %PDF-1.5↵%����↵1 0 obj↵…)
The problem is I can write a file but it opens as a blank pdf but file size is correct.
for data field I used data stream itself, blob object(var blob = new Blob([data], { type: ‘application/pdf’ }):wink: ,

This is my code

   `     $cordovaFile.writeFile(cordova.file.externalRootDirectory, "myCreatedPdf.pdf", blob, true)
        .then(function(success){
           console.log("file");
        `$cordovaFileOpener2.open(
                success.target.localURL,'application/pdf'
            );
        },function (error) {
            console.log("error");
        })`

Please help me out… any ideas…?

I found solution. I used arrayBuffer instead of blob. then it works fine

Hi udutha,

Can you explain more? How did you use the arrayBuffer ? I have a base64 image that I need to convert to file.jpg to share it.

would you post the working code, i’m stuck on it.

hi pcr,

i am beginner in ionic framework and angular

i need all sdcard files in device

i install cordova file plugin

and this is my code

window.resolveLocalFileSystemURI(cordova.file.externalRootDirectory, function(fileSystem) {
var directoryReader = fileSystem.createReader();
directoryReader.readEntries(function(entries) {
//alert(JSON.stringify(directoryReader));
for(i=0;i<entries.length;i++)
{
console.log(JSON.stringify(entries[i].name));

 }
 //alert(entries);
 //console.log(entries);

});
});

and also i add config.xml

but still i access device storage files only how do i access sdcard files using cordova plugin file

Kindly Pls advice me.

Thanks & Regards.

Hi

I am trying to use writeFile in Ionic on Android. It works writing the file to the directory
Android/data/com.ionicframework.myappname/cache but it does not work for the directory
Android/data/com.ionicframework.myappname/files
The paths are correct as I checked using alert. Is there a permission problem? The files directory is empty.

Thanks in advance

I too am facing the same issue, Please Help Guys

how did you implemeted File Reading?