Writing to a file in a Windows 10 Cordova app

While writing to a file is easy in C# applications, for apps written in JavaScript things are a bit more complicated. Especially if you want to write to a file outside of the app’s sandbox. Fortunately this can be solved using Microsoft’s WinJS libraries.

var exportData = "String of data";
var savePicker = new Windows.Storage.Pickers.FileSavePicker();
savePicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.documentsLibrary;
savePicker.fileTypeChoices.insert("CSV", [".csv"]);
savePicker.suggestedFileName = "Export.csv";

savePicker.pickSaveFileAsync().then(function (file) {
    if (file) {
        Windows.Storage.FileIO.writeTextAsync(file, exportData).done(function () {
            Windows.Storage.CachedFileManager.completeUpdatesAsync(file).done(function (updateStatus) {
                if (updateStatus === Windows.Storage.Provider.FileUpdateStatus.complete) {
                    //file saved
                } else {
                   //file not saved, failed for some reason
    } else {
        //file not saved, canceled by user

This will allow Cordova apps to write out to a part of the operating system where users can easily access the files.