Created
June 22, 2023 02:16
-
-
Save carloskim123/71cfce6a6e45f406c5a0c30d84b5820b to your computer and use it in GitHub Desktop.
File System Api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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