Skip to content

Instantly share code, notes, and snippets.

View msulais's full-sized avatar
🔥
I wonder why ...

Muhammad Sulais msulais

🔥
I wonder why ...
View GitHub Profile
@msulais
msulais / file.ts
Last active October 5, 2024 14:01
Open file in Typescript without <input type=file>
async function openFile(
accept: string | null, // file type (see https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/accept)
multiple: boolean = false,
capture?: string | null // see https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/capture
): Promise<FileList | null> {
return new Promise<FileList | null>((ok) => {
const filePickerRef = document.createElement('input')
filePickerRef.type = 'file'
if (accept != null) filePickerRef.accept = accept
if (capture != null) filePickerRef.capture = capture