[NG Cordova] $cordovaFile.readFile fails

Im using the following code:

console.log('getPostsFromFile');
$cordovaFile.readFile(me.getCachePath())
.then(function(result) {
    console.log("Result de getPostsFromFile");
    console.log(result);
    posts = JSON.parse(result);
    me.callCallback();
}, me.getPostsFromWeb);

and the code just has no output. Nothing as success or as fail. Just call the readFile and nothing more happens here.
Anyone had the same? Any idea how to solve?

Is there a ngCordova forum for that?

Iā€™m struggling with the same thing.

@jorre are you using absolute path? I think that this was my error.

Yes I am, this is my path: file:///var/mobile/Containers/Data/Application/98BDEB32-05E5-43A0-ACF4-FCF5E15E4605/tmp/cdv_photo_023.jpg

You can see my related question here with more code: ngCordova readAsText not working

What kind of path does work?
Thanks for helping out!

try only the file name

only ā€œcdv_photo_023.jpgā€ ?

That gives me:

FileError
code: 1

Mozilla seems to say this means the following:

NOT_FOUND_ERR	1	A required file or directory could not be found at the time an operation was processed. For example, a file did not exist but was being opened.

Iā€™m pretty sure the file exists, as I can show the image in my ionc app and I can also read out the file size as you can see in my code. Any other ideas or maybe a working code sample you could provide?

$cordovaFile.readAsText(filename).then(function(result) {
                console.log('readAsText Success');
                items = JSON.parse(result);
                if(items.length > 0)
                    callGetItemsCallback();
                else
                    getFromWeb(true)
            }, function() {
                console.log('readAsText Failed');
                getFromWeb(true);
            });

Thanks sid. In the end, the issue was that I couldnā€™t read/parse image contents via readAsText. I have rewritten my application without the need to parse and read in the image file so that solved my issue. Learned a lot about the file plugin.

Thanks for helping out!

Sorry. What did you learn about plugin file. I canā€™t see how you finally solve the problem. Please tell me your solution to read the file.

Hi @pcr, the problem was that I tried parsing an image by using readAsText. That doesnā€™t work on images. Iā€™m using avatars.io to upload avatars and the package only need a reference to my filename (not the fully parsed file contents). What I did was simply point to the file path the camera plugin returned and upload that directly.

Does that help? What are you trying to do exactly?

Iā€™m in the final of preparing an article to discribe how to deal with ngCordovaFile functions. I almost solve all problems. I will write some examples how they work.
Also I have some proposals to add/change functions of ngCordova.

Please give me a couple of daysā€¦

3 Likes

none of the $cordovaFile method works for me with file://fullPath from $cordovaCamera (FileURI) or $cordovaCapture (mediaFiles[i].fullPath)

1 Like

read the code I post here:

http://forum.ionicframework.com/t/how-to-write-data-to-a-file-using-ngcordova/10943/7?u=pcr

Tried your code.
The dowloadFile() part does work or at least it thinks the download is completed.
However when I try to access the downloaded file via checkFile or readFile, it returns error code 5.

   $cordovaFile
    .downloadFile(source, filePath, true, options)
    .then(function(result) {
        **// tried checkFile, readFile with filePath, result.fullPath which is /image.png, result.nativeURL which is file://var/mobile/Containers/Data/Application/xxxxx/Library/Caches/image.png They all return error code 5**
        $cordovaFile.checkFile(filePath).then(function(result) {
        // Success!
        }, function(err) {
        // An error occurred. Show a message to the user
        $timeout(function(){
            alert(err.code);
        }, 100);
        });
    }, function(err) {
    // An error occurred. Show a message to the user
    });

I end up with error code 5

Please tell me the filePath you are using in downloadFile and Iā€™ll help you to solve this issue.
$cordovaFile.checkfile doesnā€™t work.
In my apps I use window.resolveLocalFileSystemURI(Url, resOnSuccess, resOnError) to check a file exists.

Tried with

The orignial path for downloading image: filePath = cordova.file.cacheDirectory + ā€˜image.pngā€™;
or the downloadResult.nativeURL which is
file:///var/mobile/Containers/Data/Application/{app id}/Library/Caches/image.png
Error code: 5


Tried with downloadResult.fullPath which is just /image.png
Error code: 1


I donā€™t see other pathes from JSON.stringify(downloadResult) other than fullPath or nativeURL

I posted in this thread a solution for working with cordova FileTransfer and file functions together. If you use other filePath structures I canā€™ t help you. Sorry!

Read the code I post here:

Turns out it only takes cdvfile:// rather than system path file://