HTML and CSS Reference
In-Depth Information
This means that the file picker is trying to create a file but it finds out that a file with the same
name already exists. The picker then asks advice. Let's have a look at the API required to create a new
file programmatically. The following example provides an alternate route to pick a file. Instead of
using a File Save Picker, you pick a folder and then create a file programmatically:
TodoList.invokeFolderPicker = function (task) {
var folderPicker = new Windows.Storage.Pickers.FolderPicker();
folderPicker.fileTypeFilter.replaceAll(["*"]);
folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.
desktop;
// Invoke the folder picker
var fileOptions = Windows.Storage.CreationCollisionOption;
var io = Windows.Storage.FileIO;
folderPicker
.pickSingleFolderAsync()
.done(
function (folder) {
if (folder) {
folder.createFileAsync("sample.todo", fileOptions.replaceExisting)
.done(function (file) {
io.writeTextAsync(file, "Some more data.")
.then(function () { io.appendTextAsync(file, " By me."); })
.done(function () { TodoList.alert("All done!")})
},
function (error) {
TodoList.alert("Unable to create file. Sorry about that!");
});
}
});
}
Once you get a folder object, you can call the
createFileAsync
method, which takes the file name
and some options. In particular, the
Windows.Storage.CreationCollisionOption
enumeration includes
values to silently replace the file if it exists (
replaceExisting
) or fail if a name collision is detected
(
failIfExists
).
In the
done
method which runs once the file has been created, you put the code to write text.
Again the
done
method can be used to trigger any subsequent code, such as displaying an all-done
message to the user.
Search WWH ::
Custom Search