Created
February 21, 2025 09:43
-
-
Save Cheaterman/deb8e231b82488cf4ca16839894075ee to your computer and use it in GitHub Desktop.
utils/file.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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