Skip to content

Instantly share code, notes, and snippets.

@neysidev
Created July 14, 2022 19:35
Show Gist options
  • Select an option

  • Save neysidev/61414f6b139b3b7e6db4b6f1bd0a0de1 to your computer and use it in GitHub Desktop.

Select an option

Save neysidev/61414f6b139b3b7e6db4b6f1bd0a0de1 to your computer and use it in GitHub Desktop.
import _ from "lodash"
import { formatBytes } from "./format" // https://gist.github.com/neysidev/71d6100023c4359a5cff256f2601c333
const openBrowseWindow = ({
accept = [],
multiple = false,
onChange = () => {},
}) => {
// Initialize the file input
const input = document.createElement("input")
input.type = "file"
input.multiple = multiple
input.accept = accept.join(",")
// Add the event listener
input.addEventListener("change", event => {
// convert file list to array
const filesArray = _.toArray(event.target.files)
const files = filesArray.map(file => ({
...file,
size: formatBytes(file.size),
extension: file.name.split(".").at(-1),
url: URL.createObjectURL(file),
}))
onChange(files)
})
// Open the file input
input.click()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment