File System API - not standardized and not approved by the W3C API, which will only work in browsers on the Blink engine. This option will suit you only if you are writing an add-on for Chrome or a web application for a specific browser.
If not, use the standardized FileReader API , which works on almost all modern browsers .
Example of writing and reading a file (or several):
HTML:
<input id="input" type="file"> <hr> <textarea id="textarea"></textarea> <hr> <a id="download" href="">Скачать файл</a>
Javascript:
var inputElement = document.getElementById('input'), downloadElement = document.getElementById('download'), textareaElement = document.getElementById('textarea'); downloadElement.style.display = 'none'; inputElement.onchange = function() { var fileList = this.files, // список указанных файлов textFile = fileList[0]; // для нашего примера берём один файл // Проверяем тип файла (текстовой файл) if (textFile.type == 'text/plain') { // Создаём новый FileReader, который и будет читать наш файл var reader = new FileReader(); // Событие успешного чтения reader.onloadend = function(event) { var text = event.target.result, blob = new Blob([text], { type: 'text/plain' }); // Ваши любые манипуляции с данными textareaElement.value = text; downloadElement.style.display = 'block'; downloadElement.href = URL.createObjectURL(blob); downloadElement.download = textFile.name; }; // Событие ошибки reader.onerror = function() { alert('Ошибка чтения файла!'); }; // Читаем наш файл как текст reader.readAsText(textFile); } else { alert('Это не текстовой файл!'); } }; textareaElement.onkeyup = function() { // Обновляем ссылку скачивания файла // (ТОЛЬКО ДЛЯ ПРИМЕРА, СКРИПТ ВЫПОЛНЯЕТСЯ ПРИ КАЖДОМ НАЖАТИИ КЛАВИШИ) var text = this.value, blob = new Blob([text], { type: 'text/plain' }); downloadElement.href = URL.createObjectURL(blob); };
P.S. I can’t add more than two links to the answer (due to a reputation limit), but a search on "FileReader API" will give you many additional hints and examples;)