Open file from local system and save changes to file that opened
https://manajmnt-code.blogspot.com/2021/04/open-file-from-local-system-and-save.html
In this code we will be learn how to use The File System Access API to open file from local system and save changes to file that opened,
in this example we are try with txt file:
to save changes with keybord event
return to first exemple:
in this example we are try with txt file:
Ctrl + s add this code as follows:
window.addEventListener("keydown" , e => {
if (e.key === 's' && e.metaKey) {
e.preventDefault();
savefile();
}
});
return to first exemple:
let editor = document.getElementById("editor");
const openfile = async () => {
[fileHandle] = await window.showOpenFilePicker();
const file = await fileHandle.getFile();
const contents = await file.text();
editor.value = contents;
}
const savefile = async () => {
const writable = await fileHandle.createWritable();
await writable.write(editor.value);
await writable.close();
}
window.addEventListener("keydown" , e => {
if (e.key === 's' && e.metaKey) {
e.preventDefault();
savefile();
}
});