FilePicker
The FilePicker element extends the View interface to select one or more files.
To create a FilePicker, the function is used:
func NewFilePicker(session Session, params Params) FilePicker
The boolean property "multiple" (constant Multiple) is used to set the mode of selecting multiple files.The value "true" enables the selection of multiple files, "false" enables the selection of a single file.The default is "false".
You can restrict the selection to only certain types of files. To do this, use the "accept" property (constant Accept).This property is assigned a list of allowed file extensions and / or mime-types.The value can be specified either as a string (elements are separated by commas) or as an array of strings. Examples
rui.Set(view, "myFilePicker", rui.Accept, "png, jpg, jpeg") rui.Set(view, "myFilePicker", rui.Accept, []string{"png", "jpg", "jpeg"}) rui.Set(view, "myFilePicker", rui.Accept, "image/*") Two functions of the FilePicker interface are used to access the selected files:Files() []FileInfo LoadFile(file FileInfo, result func(FileInfo, []byte))
as well as the corresponding global functions
func GetFilePickerFiles(view View, subviewID ...string) []FileInfo func LoadFilePickerFile(view View, subviewID string, file FileInfo, result func(FileInfo, []byte))
The Files/GetFilePickerFiles functions return a list of the selected files as a slice of FileInfo structures.The FileInfo structure is declared as
type FileInfo struct { // Name - the file's name. Name string // LastModified specifying the date and time at which the file was last modified LastModified time.Time // Size - the size of the file in bytes. Size int64 // MimeType - the file's MIME type. MimeType string }
FileInfo contains only information about the file, not the file content.The LoadFile/LoadFilePickerFile function allows you to load the contents of one of the selected files.The LoadFile function is asynchronous. After loading, the contents of the selected file are passed to the argument-function of the LoadFile.Example
if filePicker := rui.FilePickerByID(view, "myFilePicker"); filePicker != nil { if files := filePicker.Files(); len(files) > 0 { filePicker.LoadFile(files[0], func(file rui.FileInfo, data []byte) { if data != nil { // ... } }) } }
equivalent to
if files := rui.GetFilePickerFiles(view, "myFilePicker"); len(files) > 0 { rui.LoadFilePickerFile(view, "myFilePicker", files[0], func(file rui.FileInfo, data []byte) { if data != nil { // ... } }) }
If an error occurs while loading the file, the data value passed to the result function will be nil,and the error description will be written to the log
The "file-selected-event" event (constant FileSelectedEvent) is used to track changes in the list of selected files.The main event listener has the following format:
func(picker FilePicker, files []FileInfo))
where the second argument is the new value of the list of selected files.
You can get the current list of listeners of the list of files changing using the function
func GetFileSelectedListeners(view View, subviewID ...string) []func(FilePicker, []FileInfo)