Skip to content

Instantly share code, notes, and snippets.

@NickyMeuleman
Created June 4, 2022 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NickyMeuleman/2641d4004addf3f4f68cb2eb2e87d62d to your computer and use it in GitHub Desktop.
Save NickyMeuleman/2641d4004addf3f4f68cb2eb2e87d62d to your computer and use it in GitHub Desktop.
File System Access API append to file
<!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>
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