【问题标题】:WP8 / Cordova filesystem - does anyone know the correct code?WP8 / Cordova 文件系统 - 有人知道正确的代码吗?
【发布时间】:2014-10-01 22:36:44
【问题描述】:

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

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

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 票 :) 只是猜测,但您是否确保授予您的应用访问文件系统的权限?
  • 你的问题正好说明了我的观点!我将在哪里/如何检查或启用它?
  • 我试过了,但fileSystem.root.valueundefined。这是否仅适用于设备而不适用于模拟器?

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


【解决方案1】:

与我上面的评论有关,但想找个地方放代码...

我自己没有做过 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,所以也许这不是问题......我认为上面的链接可能有用。

【讨论】:

  • 这正是答案。将 ID_CAP_REMOVABLE_STORAGE 添加到 WPAppManifest.xml 允许我的第二个代码示例运行。注意 Apache Cordova;启用 WP8 文件系统时,还要更新清单。感谢@CodingWithSpike 解决了这个问题。
  • 23 小时后,不幸的是,这似乎不是全部答案 :(。window.requestFileSystem 异步请求在 WP8 上似乎非常慢并且将返回未定义 - 所以我已经移动了该请求到应用程序加载事件(使用承诺会更好)。然而 WP8 仍然拒绝遵守所有其他 Cordova 平台文件系统插件。
  • 添加 ID_CAP_REMOVABLE_STORAGE 对我有用,而不是文件 api 效果很好。谢谢
猜你喜欢
  • 2015-10-16
  • 1970-01-01
  • 2016-01-03
  • 2023-03-18
  • 2012-05-22
  • 1970-01-01
  • 2011-07-28
  • 2010-09-06
相关资源
最近更新 更多