我正在使用HTML5开发Windows 8应用程序。该应用程序需要一些用户数据来创建新事件。该活动有标题,描述,日期和时间。我希望在用户填写表单后将此数据以JSON格式存储在文本文件中,以便下次用户打开应用程序时可以看到它。
我可以找到将JSON对象写入文本文件的不同方法,但是我找不到与在文本文件末尾附加数据相关的任何内容。基本上,我有一个events.txt,我从中读取并显示数据,并且我想在用户添加事件后将事件附加到相同的.txt。
我的events.txt看起来像这样
[
{
"key": "event1",
"title": "title1",
"description": "description1",
"date": "<sample date>",
"time": "<sample time>",
"backgroundImage": "../images/logo.png"
}
]
我正在使用以下代码
从此文件中读取事件WinJS.xhr({ url: "../data/events.txt" }).then(function (xhr) {
var events = JSON.parse(xhr.responseText);
events.forEach(function (feed) {
event.push({
group: feed,
key: feed.key,
title: feed.title,
description: feed.description,
date: feed.date,
time: feed.time,
backgroundImage: feed.backgroundImage
});
});
});
我会非常感激任何形式的帮助,因为我在这里的最后期限紧张。
感谢。
答案 0 :(得分:4)
您是否在寻找Consumer Preview
的{{3}}?
如果我正确理解了您的问题,我认为这里有一些您想要使用的重要API。
// Create a file
var eventFile;
Windows.Storage.KnownFolders.documentsLibrary.createFileAsync("event.txt", Windows.Storage.CreationCollisionOption.openIfExists).done(function (file) {
eventFile = file;
});
// Write Text to a file
Windows.Storage.FileIO.writeTextAsync(eventFile, JSON.stringify(yourJSONObject));
// Append Text to a file
Windows.Storage.FileIO.appendTextAsync(eventFile, JSON.stringify(yourJSONObject));
对FileIO API的更多参考: File Access sample
此外,您为什么不考虑使用local storage
或indexedDB
存储您的活动信息?