Metro Style App HTML/JS编写本地文件 [英] Metro Style App HTML/JS writing local files

查看:76
本文介绍了Metro Style App HTML/JS编写本地文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为Windows应用商店构建了一个应用程序,该应用程序使用Windows.Storage.FileIO(createFileAsync,readTextAsync和writeTextAsync)写入本地文件.

I've built an app for Windows Store that writes to local files using Windows.Storage.FileIO (createFileAsync, readTextAsync and writeTextAsync).

我的计划是将该文件用作简单的数据库(数据只是JSON格式的用户信息).这是个坏主意吗?

My plan is to use the file as a simple database (the data is simply JSON formatted user information). Is this a bad idea?

以这种方式写入的文件是否真正持久(或者在更新应用程序时将其删除)?用户可以自己删除它们吗(例如通过删除cookie/临时Internet文件等)?

Are the files written this way really persistent (or are they deleted when the app is updated)? Can the user themselves delete them (for example through deleting cookies/temp internet files or the likes)?

我只是想知道"Metro"环境如何处理由应用程序本地写入磁盘的文件...

I'm just wondering how files written to the disk locally by apps are treated by the "Metro" environment...

推荐答案

是否是坏主意"的问题很主观,因此可能不是正确的选择.

The question of whether it's a "bad idea" is quite subjective, so probably not the right venue for that.

但是,就Windows Store应用程序如何对待存储的文件而言,如果使用Windows.Storage.ApplicationData.current.localFolder创建和/或编辑文件,则这些文件将一直可用,直到用户卸载该应用程序为止.如果您将应用程序的更新推送到商店中,并且用户下载并安装了更新,则文件将保留在用户的计算机上,除非您通过编程方式明确地对其进行了修改.

However, in terms of how Windows Store apps treat files you store, if you create and/or edit files using Windows.Storage.ApplicationData.current.localFolder, these files will be available until the user uninstalls the app. If you push an update of your app to the store, and the user downloads and installs the update, the files will remain on the user's machine, unless you explicitly modify them programmatically.

在我构建的两个应用程序中,我利用localFolder存储应用程序数据的JSON表示形式,这样我可以最小化启动时间,并在应用程序启动并呈现其初始UI时在后台刷新数据给用户.

In two apps that I've built, I leverage the localFolder to store JSON representations of the app's data so that I can minimize startup time, and refresh the data in the background once the app has started up and rendered its initial UI to the user.

这是我的代码(可能会有所优化)如下所示:

Here's what my code (which could probably be optimized a bit) looks like:

appData.current.localFolder.createFileAsync("leaderboard.txt", 
    Windows.Storage.CreationCollisionOption.openIfExists)
.then(function (file) {
    leaderboardFile = file;
    return Windows.Storage.FileIO.readTextAsync(file)
})
.then(function (fileContents) {
    cachedLeaderboard = fileContents;
    if (!cachedLeaderboard || isRefresh) {
        return WinJS.xhr(xhrOptions)
    }
    else {
        cachedLeaderboard = JSON.parse(cachedLeaderboard);
        List = new WinJS.Binding.List(cachedLeaderboard);
        completed(List);
    }
})
.then(function (result) {
    if (result) {
       var items = JSON.parse(result.responseText).d;
       localLeaderboard = items;
       return Windows.Storage.FileIO.writeTextAsync(leaderboardFile, 
           JSON.stringify(localLeaderboard))
    }
})
.then(function () {
    if (!localLeaderboard)
    localLeaderboard = cachedLeaderboard;
    List = new WinJS.Binding.List(localLeaderboard);
    completed(List);
})

基本上,代码将检查leaderboard.txt文件是否存在(或者该调用是否刷新了数据),如果不存在,则进行XHR调用以获取数据,然后创建绑定列表并传递它回到包装在承诺中的调用者(未显示承诺代码).如果文件存在,并且请求没有刷新,我只需解析文本文件并从中创建绑定列表.

Basically, the code will check whether the leaderboard.txt file exists (or if the call is a refresh of the data), and if not, make an XHR call to get the data, then create a binding list and pass it back to the caller wrapped in a promise (the promise code isn't shown). If the file exists, and the request isn't a refresh, I just parse the text file and create the binding list from that.

希望有帮助!

这篇关于Metro Style App HTML/JS编写本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆