Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.showOpenFilePicker polyfill

Is there a polyfill for window.showOpenFilePicker (MDN)?

like image 762
Lenard Avatar asked Aug 06 '26 02:08

Lenard


1 Answers

function showOpenFilePickerPolyfill(options) {
    return new Promise((resolve) => {
        const input = document.createElement("input");
        input.type = "file";
        input.multiple = options.multiple;
        input.accept = options.types
            .map((type) => type.accept)
            .flatMap((inst) => Object.keys(inst).flatMap((key) => inst[key]))
            .join(",");

        input.addEventListener("change", () => {
            resolve(
                [...input.files].map((file) => {
                    return {
                        getFile: async () =>
                            new Promise((resolve) => {
                                resolve(file);
                            }),
                    };
                })
            );
        });

        input.click();
    });
}

if (typeof window.showOpenFilePicker !== 'function') {
    window.showOpenFilePicker = showOpenFilePickerPolyfill
}

like image 164
Lenard Avatar answered Aug 07 '26 14:08

Lenard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!