Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WP8 / Cordova filesystem - does anyone know the correct code?

There is a serious lack of documentation on how to use the Cordova file plugin with the WP8 platform.

I've got an app on Android, fireOS and iOS, all using the file plugin to view directory contents, download, save and open generated files from my webservice which all use the following code:

function listDir() {
//console.log('listDir');
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);

function gotFS(fileSystem) {
    //console.log('gotFS. filesystem.root = ' + fileSystem.root.value);
    fileSystem.root.getDirectory("MyFolder", { create: true, exclusive: false }, gotDir);

}

function gotDir(dirEntry) {
    //console.log('gotDir');
    // Get a directory reader
    var directoryReader = dirEntry.createReader();

    // Get a list of all the entries in the directory
    directoryReader.readEntries(success, fail);

}

function success(entries) {
    var i = 0, sb = '';
    sb += '<ul data-role="listview" data-inset="true" id="pdfFiles">';
    if (entries.length > 0) {
        for (i = 0; i < entries.length; i++) {
            sb += '<li><a href="#" data-src="' + entries[i].toURL() + '"><img src="images/icons/icon_pdf.png" class="ui-li-icon" width="16px" height="16px" alt="PDF Icon" />';
            sb += entries[i].name;
            //sb += '<br />';
            //sb += entries[i].fullPath;
            sb += '</a></li>';
        }
    } else {
        sb += '<li><p>You do not have any saved reports</p></li>';
    }

    sb += '</ul>';
    $('#pdfReports-entries').html(sb);
    $('ul#pdfFiles').listview().listview('refresh');

    //open the pdf file using the fileOpener plugin
    $('ul#pdfFiles li a').on('click', function () {

        $this = $(this);
        window.plugins.fileOpener.open($this.attr('data-src'));
    });
}

function fail(error) {
    logError("Failed to list directory contents: " + error.code);
    sb += '<ul data-role="listview" data-inset="true" id="pdfFiles">';
    sb += '<li><p>You do not have any saved reports</p></li>';

    sb += '</ul>';
    $('#pdfReports-entries').html(sb);
    $('ul#pdfFiles').listview().listview('refresh');
}

}

WP8 throws the following error at the gotFS function:

A first chance exception of type 'System.IO.IsolatedStorage.IsolatedStorageException' occurred in mscorlib.ni.dll

I then tried the code at this Github, which still failed to create or read any directories, but didn't throw the IsolatedStorageException exception.

I've asked Google many times, but it is unable to give a coherent answer.

Does anyone know how to use the file plugin with WP8?

like image 528
DaveSav Avatar asked Nov 01 '22 16:11

DaveSav


1 Answers

Related to my comment above, but wanted a place to put code...

I haven't done a WP8 app myself, only iOS and Android, but maybe the app doesn't have the right permissions?

These would go in your Properties/WPAppManifest.xml file, and look like:

<Capabilities>
    <Capability Name="ID_CAP_WEBBROWSERCOMPONENT" />
    <Capability Name="ID_CAP_IDENTITY_DEVICE" />
    <Capability Name="ID_CAP_IDENTITY_USER" />
</Capabilities>

And the list of available capability IDs is listed here on MSDN. Though the only one I see related to file storage is ID_CAP_REMOVABLE_STORAGE so maybe that isn't the issue... I figured the link above might be useful though.

like image 164
CodingWithSpike Avatar answered Nov 11 '22 07:11

CodingWithSpike