Skip to content

Instantly share code, notes, and snippets.

@carloskim123
Created June 22, 2023 02:16
Show Gist options
  • Save carloskim123/71cfce6a6e45f406c5a0c30d84b5820b to your computer and use it in GitHub Desktop.
Save carloskim123/71cfce6a6e45f406c5a0c30d84b5820b to your computer and use it in GitHub Desktop.
File System Api
const fileContentDiv = document.getElementById('file-content');
let fileHandle;
const pickerOpts = {
types: [
{
description: "Images",
accept: {
"image/*": [".png", ".gif", ".jpeg", ".jpg", ".js", ".html", ".css", ".go"],
},
},
],
excludeAcceptAllOption: true,
multiple: false,
}
const button = async () => {
[fileHandle] = await window.showOpenFilePicker(pickerOpts);
let fileData = await fileHandle.getFile();
let text = await fileData.text();
fileContentDiv.textContent = text;
}
const save = async () => {
let stream = await fileHandle.createWritable();
await stream.write(fileContentDiv.textContent)
await stream.close();
}
const saveAs = async () => {
fileHandle = await window.showSaveFilePicker();
save()
}
document.body.style.background = 'darkviolet';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment