Is there any plans to support window.showOpenFilePicker

Hello,

I have build a few Haxe projects for internal use and am still learning. I did try find if this question has been raised before so apologies if it is duplicate but I could not find it.

I have started a Game tool editing project and I could switch from HTML to other compilations but really HTML would be really great if I can get it all done there.

The project is Kha-HTML5 using haxeui.

I am trying to add my own support for window.showOpenFilePicker but would like to know if that support has been considered in any future versions.

Thank you

For any others who might want something similar this is my Hacked together solution.

There are two files.

of.js

of={
    showOpenFilePicker:async function(description,mimeType,extensions,excludeAcceptAllOption=true,openFunction){
        const pickerOpts = {
            types: [
                {
                    description: description,
                    accept: {
                        [mimeType]: extensions
                    }
                },
            ],
            excludeAcceptAllOption: excludeAcceptAllOption,
            multiple: false
        };

        const [fileHandle] = await window.showOpenFilePicker(pickerOpts);

        const fileData = await fileHandle.getFile();

        openFunction(fileData);
    }
}

Javascript function to open a file. This must be imported in your base html.
<script src="of.js"></script>

Of.hx

package of;

import js.html.File;
import haxe.Constraints.Function;

@:native("of")
extern class Of {
    static function showOpenFilePicker(description:String,mimeType:String,extensions:Array<String>,excludeAcceptAllOption:Bool,oopenFunction:Function):File;
}

Haxe class to represent the javascript file.

Usage

To use this you need to be responding to a user event. This is a security requirement from the browser.

Sample Use


    @:bind(menuFileOpen, MouseEvent.CLICK)
    private function fileOpen(e:MouseEvent) {
        #if kha_html5
        Of.showOpenFilePicker("GDT Project","json/application",[".json"],true,function(file:File){
            trace(file.name);
            trace(file.type);
            trace(file.size);
            trace(file.lastModified);
            
            var fr:FileReader=new FileReader();
            fr.onloadend=function () {
                trace(fr.result);
            }
            fr.readAsBinaryString(file);
        });
        #end
    }

Comments

Brave

This feature is reasonably new and currently if you use brave its locked behind a flag. You can read about the issue here Allow folks to enable File System API with a Flag · Issue #18979 · brave/brave-browser · GitHub

Enhancements

  • Extend to support returning Directories.
  • Allow the returning of multiple Files.
  • Allow multiple types of file filters as apposed to just one.
  • Have a callback for if the user choose not to select a file.

Hasn’t open file be added to haxeui ? It seems to be the case.
http://haxeui.org/explorer/#containers/dialogs/file_dialogs

Try using the most recent haxeui git versions.

2 Likes

Thank you for pointing that out. I was looking so hard for a window.showOpenFilePicker I didn’t look further and find this implementation.