Search code examples
cordovawindows-phonecordova-pluginsvisual-studio-cordova

Cordova file reader don't work


I have a strange problem with the File Plugin in Cordova. The following code works fine if I run it as windows phone 8.1 app. But it fails if I run it as Windows Phone Universal App

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
       function (fs) {
           fs.root.getFile("www/test.xml", null, function (fileEntry) {
               fileEntry.file(gotFile, fail);
           }, fail);
       }, fail);

After the call of

function (fs) {
           fs.root.getFile("www/test.xml", null, function (fileEntry) {
               fileEntry.file(gotFile, fail);
           }, fail);

The Error Callback function is called with error code 1. This means, that the file isn't found. Does anyone have an idea whats the problem? And if so, how can I solve it.

I think, that I am in the wrong filesystem. Does anyone know which I have to use?

I know that there are some similiar questions, but they refer only to windows phone 8.1 and not to windows phone universal. And I am using cordova in Visual Studio.


Solution

  • I couldn't get it work as windows phone 8.1 app either.

    I tried it with {create:true} using following codes:

    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
        function (fs) {
           fs.root.getFile("test.xml", { create: true }, function (fileEntry) {
               fileEntry.file(gotFile, fail);
           }, fail);
    }, fail);
    

    and I got the fileEntry like below: enter image description here

    As you can see the nativeURL of the newly created file is ms-appdata:///local//test.xml,which means it was not created under application directory, but under C:\Users\<username>\AppData\Local\Packages\<application identity name>\LocalState like below:

    enter image description here

    I researched the documentation but didn't find a way to retrieve the applicaiton directory files. So currently the workaround is to use windows API to get the file under application directory like below:

    if (cordova.platformId == "windows")
    {
        var uri = new Windows.Foundation.Uri("ms-appx:///www/test.xml");
        Windows.Storage.StorageFile.getFileFromApplicationUriAsync(uri).then(function (fs) {
                    var abc = fs;//get the file here
                 },fail);
     }