Skip to content

Instantly share code, notes, and snippets.

@Cheaterman
Created February 21, 2025 09:43
Show Gist options
  • Save Cheaterman/deb8e231b82488cf4ca16839894075ee to your computer and use it in GitHub Desktop.
Save Cheaterman/deb8e231b82488cf4ca16839894075ee to your computer and use it in GitHub Desktop.
utils/file.ts
export function selectFile(mimeTypes: string[] = [], multiple: boolean = false) {
return new Promise<FileList | null>((resolve, reject) => {
const input = document.createElement('input')
input.type = 'file'
input.multiple = multiple
if (mimeTypes.length) {
input.accept = mimeTypes.join(', ')
}
input.click()
input.addEventListener(
'change',
(event: Event) => {
const { files } = event.target as HTMLInputElement
resolve(files)
}
)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment