【发布时间】:2017-08-18 15:54:46
【问题描述】:
我有一个文件保存功能:
function WINJSWrite(file, content) {
if (content.length <= 0) {
GameConsts.MyAlert("No data to save.");
}
// Prevent updates to the remote version of the file until we finish making changes and call CompleteUpdatesAsync.
Windows.Storage.CachedFileManager.deferUpdates(file);
// write to file
Windows.Storage.FileIO.writeTextAsync(file, content).done(function () {
// Let Windows know that we're finished changing the file so the other app can update the remote version of the file.
// Completing updates may require Windows to ask for user input.
Windows.Storage.CachedFileManager.completeUpdatesAsync(file).done(function (updateStatus) {
if (updateStatus === Windows.Storage.Provider.FileUpdateStatus.complete) {
//WinJS.log && WinJS.log("File " + file.name + " was saved.", "sample", "status");
} else {
//WinJS.log && WinJS.log("File " + file.name + " couldn't be saved.", "sample", "status");
}
});
});
};
那我这样称呼它,取决于myfile.txt是否存在:
localFolder.getFileAsync('myfile.txt').then(
function (file) {
// NEVER GETS HERE
if (file) {
WINJSWrite(file, content);
} else {
localFolder.createFileAsync("myfile.txt").done(
function (newFile) {
if (newFile) {
WINJSWrite(newFile, content);
}
});
}
});
但代码永远不会到达// NEVER GETS HERE。
我做错了什么?
注意:如果我使用localFolder.getFileAsync('myfile.txt').done( 而不是localFolder.getFileAsync('myfile.txt').then(,则会引发错误,指出文件不存在(但我知道它存在)。
【问题讨论】:
标签: javascript file-io uwp win-universal-app