Where can I find the file saved using $cordovaFile.writeFile

I am trying to save a generated html file in my ionic app to my android device. I am using $cordovaFile to try and write the file using the following code:

var fileName = "Order" + $scope.header.orderno + ".html";
document.addEventListener('deviceready', function () {

    // WRITE
    $cordovaFile.writeFile(cordova.file.dataDirectory, fileName, $scope.htmlToPrint, true)
      .then(function (success) {
        console.log("file printed");
        console.log(success);
      }, function (error) {
        console.log("Error");
        console.log(error);
      });

});

After execution, I am getting the “file printed” message in the console with the following success object:

ProgressEvent
 bubbles:false
 cancelBubble:false
 cancelable:false
 lengthComputable:false
 loaded:0
 target:FileWriter
  error:null
  fileName:""
  length:1461
  localURL:"cdvfile://localhost/files/Order26.html"
  onabort:null
  onerror:null
  onprogress:null
  onwrite:null
  onwriteend:(e)
  onwritestart:null
  position:1461
  readyState:2
  result:null
  __proto__:Object
  total:0
  type:"writeend"

However, I cannot find the file Order26.html in my device. Where can I find this file?

If the writeFile function is success, your file must be in cordova.file.directory + fileName location (varies for OS)

One way to check it is doing after your $cordovaFile.writeFile success callback (or in the chrome debugger console)

You must replace fileName variable for whatever the file name is if you use the chrome debugger console

window.resolveLocalFileSystemURL(cordova.file.directory + fileName, function(file){
    console.log(file);
}, function(error){
    console.error(error);
});

Of course you can also use $cordovaFile service to read the file, but this way is faster (and can be use in chrome debugger console) to debug.

Thank you for your response. I tried your code and it sent back a success response object.

filesystem:FileSystem
name:"files"
root:DirectoryEntry
 filesystem:FileSystem
 fullPath:"/"
 isDirectory:true
 isFile:false
 name:""
 nativeURL:"file:///data/data/com.tamangsistema.taraletsbackoffice/files/"
 __proto__:Entry
__proto__:Object
fullPath:"/Order26.txt"
isDirectory:false
isFile:true
name:"Order26.txt"
nativeURL:"file:///data/data/com.tamangsistema.taraletsbackoffice/files/Order26.txt"
__proto__:Entry

I tried looking at the device using windows file explorer and I can’t find it in the device (I used the seach function to look for Order26.txt). I also looked at My Files in the device but there is no file in the Device storage/Android/data/com.tamangsistema.taraletsbackoffice/files directory. By the way, I tried this with 2 devices (Samsung S4 and Samsung Tablet 3V) with the same result. I was expecting that the file will be saved somewhere within the device.

hello again!

The file was written correctly and read it without errors, but you can not see in your device with a file explorer because you wrote the file in a private location, this means that only your application can access to it. This is useful for files that you want access but not your users.

take a look to the ngCordova documentation, more especifically to the Android File System Layout section. Here you can see where in the app is the file storage, if it is public or not, if it is persistent, etc…

If you want to see the file, and let your uses to see it too… you can use cordova.file.externalDataDirectory instead

2 Likes

cool - that worked! I incorrectly assumed that the externalDataDirectory is only for SD Card storage. Thanks for your help.

2 Likes

Your solutions save me alot time man !!
Thanks !!