I build a text that I want to save on my disk. I suggest a file-name, but user can modify it. How to get the final file-name ? Where can I access to this value in my following-code ? Which element contains the final file-name ?
Best regards.
/*
*/
function saveTextOnLocalFile( textToWrite , strFileName, dotAndExt) {
// create a new Blob (html5 magic) that conatins the data from your form feild
let textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});
// Specify the name of the file to be saved
if (strFileName == null) {
strFileName = "export-" + new Date().yyyymmddhhmmss() + dotAndExt;
}
// create a link for our script to 'click'
let downloadLink = document.createElement("a");
downloadLink.download = strFileName;
// provide text for the link. This will be hidden
downloadLink.innerHTML = "My Hidden Link";
downloadLink.href = window.URL.createObjectURL( textFileAsBlob);
// when link is clicked call a function to remove it from the DOM in case user wants to save a second file.
downloadLink.onclick = destroyClickedElement;
// make sure the link is hidden.
downloadLink.style.display = "none";
// add the link to the DOM
document.body.appendChild(downloadLink);
// click the new link
downloadLink.click();
}
/**/
function destroyClickedElement(event) {
let downloadLink = event.target;
let fileName = downloadLink.download;
// remove the link from the DOM
document.body.removeChild( event.target);
}