Created
June 4, 2022 20:00
-
-
Save NickyMeuleman/2641d4004addf3f4f68cb2eb2e87d62d to your computer and use it in GitHub Desktop.
File System Access API append to file
This file contains hidden or 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Document</title> | |
</head> | |
<body> | |
<button id="choose-file">choose a file to write to</button> | |
<button id="boop">BOOP</button> | |
<button id="potato">potato</button> | |
<script src="index.js"></script> | |
</body> | |
</html> |
This file contains hidden or 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 writeButton = document.querySelector("#choose-file"); | |
let handle; | |
writeButton.addEventListener("click", async () => { | |
handle = await window.showSaveFilePicker(); | |
}); | |
const boopButton = document.querySelector("#boop"); | |
boopButton.addEventListener("click", async () => { | |
let file = await handle.getFile(); | |
const writable = await handle.createWritable({ keepExistingData: true }); | |
await writable.write({ type: "seek", position: file.size }); | |
await writable.write({ type: "write", data: "BOOP\n" }); | |
await writable.close(); | |
}); | |
const potatoButton = document.querySelector("#potato"); | |
potatoButton.addEventListener("click", async () => { | |
let file = await handle.getFile(); | |
const writable = await handle.createWritable({ keepExistingData: true }); | |
await writable.write({ type: "seek", position: file.size }); | |
await writable.write({ type: "write", data: "potato\n" }); | |
await writable.close(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment