как с помощью js сохранить данные?
Как с помощью js сохранить данные формы в отдельном текстовым файле?
Ответы (1 шт):
Автор решения: ZxNuClear
→ Ссылка
Вот пример, как это можно сделать:
JS:
let saveFile = () => {
const text1 = document.getElementById('text1');
const text2 = document.getElementById('text2');
let data =
'Text 1: ' + text1.value + '\n' +
'Text 2: ' + text2.value;
const textToBLOB = new Blob([data], { type: 'text/plain' });
const sFileName = 'file.txt';
let newLink = document.createElement("a");
newLink.download = sFileName;
if (window.webkitURL != null) {
newLink.href = window.webkitURL.createObjectURL(textToBLOB);
}
else {
newLink.href = window.URL.createObjectURL(textToBLOB);
newLink.style.display = "none";
document.body.appendChild(newLink);
}
newLink.click();
}
HTML:
<div>
<input type="text" id="text1" placeholder="text 1" />
<input type="text" id="text2" placeholder="text 2" />
<input type="button" id="btn" value="Save" onclick="saveFile()" />
</div>