Skip to content

Instantly share code, notes, and snippets.

@glenjamin
Created September 22, 2016 16:06
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 glenjamin/142b40775c0ec616e84deaa5bafd90f2 to your computer and use it in GitHub Desktop.
Save glenjamin/142b40775c0ec616e84deaa5bafd90f2 to your computer and use it in GitHub Desktop.
Open a file dialog
/**
* Get a promise for a user-selected File object
*/
export function chooseLocalFile(): Promise<File> {
const input = document.createElement("input");
input.type = "file";
input.accept = "application/json";
return new Promise((resolve) => {
input.addEventListener("change", () => {
const file = input.files[0];
resolve(file);
});
input.click();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment