WP8 / Cordova文件系统 - 有谁知道正确的代码?

时间:2014-10-01 22:36:45

标签: cordova windows-phone-8 cordova-plugins html5-filesystem

严重缺乏关于如何在WP8平台上使用Cordova文件插件的文档。

我在Android,fireOS和iOS上都有一个应用程序,都使用文件插件查看目录内容,从我的webservice下载,保存和打开生成的文件,这些文件都使用以下代码:

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在gotFS函数中抛出以下错误:

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

然后我尝试了this Github的代码,它仍然无法创建或读取任何目录,但没有抛出IsolatedStorageException异常。

我已经多次问过谷歌,但它无法给出一致的回答。

任何人是否知道如何在WP8中使用该文件插件?

1 个答案:

答案 0 :(得分:2)

与我上面的评论相关,但想要一个放置代码的地方......

我自己没有做过WP8应用程序,只有iOS和Android,但也许应用程序没有正确的权限?

这些将放在您的Properties/WPAppManifest.xml文件中,如下所示:

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

可用功能ID列表为listed here on MSDN。 虽然我看到的唯一一个与文件存储相关的是ID_CAP_REMOVABLE_STORAGE,所以也许这不是问题......我认为上面的链接可能很有用。