Objective: Upon clicking the download link, an automatically named file will be downloaded with today's date and time format. For example, 7-3-2021 04:04:00 PM or any applicable format with the date and time included in the name.
Below is the code snippet for the download link:
<a download="info.txt" id="downloadlink" style="display: none"><button class="btn btn-block btn-warning">Download</button></a>
And here is the code for the function:
(function () {
var textFile = null,
makeTextFile = function (text) {
text = text.replace(/\n/g, "\r\n");
var data = new Blob([text], {type: 'text/plain'});
// If we are replacing a previously generated file we need to
// manually revoke the object URL to avoid memory leaks.
if (textFile !== null) {
window.URL.revokeObjectURL(textFile);
}
textFile = window.URL.createObjectURL(data);
return textFile;
};
var create = document.getElementById('create'),
textbox = document.getElementById('todayGeneratedReport');
create.addEventListener('click', function () {
var link = document.getElementById('downloadlink');
link.href = makeTextFile(textbox.value);
link.style.display = 'block';
}, false);
})();
Although the code is correct, I am facing issues with implementing the date and time format for the downloaded file. Currently, the file is saved as info.txt when the link is clicked. Is it necessary to create a new function and link it to the download property of the link? Any assistance on this matter would be greatly appreciated. Thank you.