javascript - 在 Windows Metro 风格的 Javascript 应用程序中读取文件内容

标签 javascript html windows microsoft-metro windows-runtime

我已经花了好几个小时绞尽脑汁,一遍又一遍地阅读我在 MSDN 和 Windows 开发中心上找到的所有教程;没有任何效果。

我似乎无法从纯文本文件中读取数据!

是的,我已经添加了文件关联操作,是的,我已经向我的项目添加了声明。

代码,来自msdn:

(function () {
    "use strict";

    WinJS.UI.Pages.define("/pages/home/home.html", {
        // This function is called whenever a user navigates to this page. It
        // populates the page elements with the app's data.
        ready: function (element, options) {
            // TODO: Initialize the page here.


            Windows.Storage.FileIO.readTextAsync("AvailableBalance.sv").then(function (contents) {
                // Add code to process the text read from the file
                document.getElementById("AvailableBalanceText").innerText = contents;
            });
        }
    });
});

还有:

<h1 id="AvailableBalanceText"></h1>

不起作用。该应用程序加载正常 - 但只是不从文件读取(或不显示数据)。

有人可以告诉我我在这里做错了什么吗?

最佳答案

readTextAsync doesn't take a file name -- it takes an IStorageFile .

您需要在某个地方获取对该文件的引用,而不仅仅是打开它。如果它是用户文件,那么您需要使用documentsLibrary打开它:

Windows.Storage.KnownFolders.documentsLibrary.getFileAsync("sample.dat").done(function (yourFileHandle) {
    // you now have a file handle.
});

请注意,这是在文档库中。如果您需要访问包中的文件或其他位置(您无法访问 Metro 中的任意路径,只能访问库、应用程序的本地数据和包),则需要使用其他 API 之一。我建议看看 sample用于文件访问。

关于javascript - 在 Windows Metro 风格的 Javascript 应用程序中读取文件内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12184824/

相关文章:

c++ - 在 C++、Windows 7 中包含 OpenGL 库

php - javascript jquery 调用 php 无法在 iPhone/Ipad 上运行

javascript - 获取 Angular 4 中随机数的单元测试

javascript - "Uncaught RefereceError: txtfile is not defined"

php - 什么是 Open ID,我该如何使用它?

python - 确定性 python 脚本以非确定性方式运行

javascript - 模型存储未定义

javascript - 使用 src 链接获取 Img 元素

javascript - 查找具有类的兄弟元素并用 jQuery 替换 html 内容

Python 找不到我的文本文件?