I recently developed a universal application for Windows 10 with UWP web context (so JavaScript and HTML) and I would like to save a text file. It works well on a browser (Chrome, Firefox, Edge,...) but not in the application. Can someone help me? :) Thank you in advance!
Here is the code responsible for saving the text file.
function saveTextAsFile(fileName) {
var source = input.value.replace(/\n/g, "\r\n");
var fileUrl = window.URL.createObjectURL(new Blob([source], {type:"text/plain"}));
var downloadLink = createDownloadLink(fileUrl, fileName);
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);}
To download a file using a Progressive Web App as a Universal Windows Platform you can use the
Windowsglobal object with theFileSavePicker. Note that you can check to see if it exists usingif (window['Windows'] != null) { ... }This assumes you are downloading a text file. To download a
Uint8Arrayuse theWriteBytesAsyncfunction onFileIOinstead. Note that many of the functions onFileIOare available in JavaScript even though they are not documented for JavaScript.Check out the FileSavePicker Class documentation for more information.