Skip to content

Instantly share code, notes, and snippets.

@robert-hoffmann
Last active February 22, 2023 21:04
Show Gist options
  • Select an option

  • Save robert-hoffmann/c52a68cb2bfe01f717104b4a5e9912be to your computer and use it in GitHub Desktop.

Select an option

Save robert-hoffmann/c52a68cb2bfe01f717104b4a5e9912be to your computer and use it in GitHub Desktop.
upload/download files client-side only
<input type="file" onchange="fileSelected(this)" accept="application/JSON" />
<a
download="settings.json"
target="_blank"
>download</a>
<pre>
<code></code>
</pre>
const preview = document.querySelector('code');
const download = document.querySelector('a');
function fileSelected(ele) {
const file = ele.files[0];
const fr = new FileReader();
fr.onload = () => {
preview.innerText = fr.result;
download.href = window.URL.createObjectURL(file);
};
fr.readAsText(file);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment