scripting: add fileUpload function

This commit is contained in:
jean-emmanuel 2026-04-09 14:59:03 +02:00
parent 892cbd6886
commit 9d79e857f8
4 changed files with 50 additions and 4 deletions

View file

@ -7,6 +7,9 @@
- knob: add `horizontal` mode
- eq: filter responses are now only computed when needed; the widget's value is no longer exposed / sync'ed.
- scripting
- add `uploadFile` function for reading client-side files
## 1.30.2
- bug fixes

View file

@ -416,10 +416,19 @@ Returns the instance of [Navigator](https://developer.mozilla.org/en-US/docs/Web
#### `browseFile(options, callback)`
Opens the file file browser and pass selected file to a callback function (does not create nor read files by itself).
Opens the server file browser and pass selected file to a callback function (does not create nor read files by itself).
- `options`: object with the following keys:
- `extension`: allowed extension (default: "*")
- `directory`: starting directory for browser (default: user's home)
- `allowDir`: allow choosing a directory instead of a file (default: false)
- `mode`: "save" or "open"
- `callback`: function called with 1 argument (file path)
#### `uploadFile(options, callback)`
Opens the client file upload prompt and pass the raw content of selected file to a callback function.
- `options`: object with the following keys:
- `types`: equivalent of HTML attribute [`accept`](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/accept)
- `callback`: function called with 1 argument (file content)

View file

@ -12,7 +12,9 @@ export default function uiFileUpload(types, ok, error) {
if (SINGLETON) CONTAINER.removeChild(SINGLETON)
SINGLETON = html`<input type="file" accept="${types}" style="position:absolute;opacity:0;pointer-events:none;"/>`
SINGLETON = html`<input type="file" style="position:absolute;opacity:0;pointer-events:none;"/>`
if (types) SINGLETON.accept = types
CONTAINER.appendChild(SINGLETON)
@ -24,9 +26,9 @@ export default function uiFileUpload(types, ok, error) {
uiLoading(locales('loading_upload'))
reader.onerror = reader.onabort = function() {
reader.onerror = reader.onabort = function(e) {
uiLoading(false)
error()
error(e)
}
reader.onload = function(e) {

View file

@ -6,6 +6,7 @@ import {urlParser} from '../utils.mjs'
import Vm from '../vm.mjs'
import ipc from '../../ipc/index.mjs'
import uiFilebrowser from '../../ui/ui-filebrowser.mjs'
import uiFileUpload from '../../ui/ui-fileupload.mjs'
import {setFOCUSABLE} from '../../globals.mjs'
var toolbar
@ -627,6 +628,37 @@ class ScriptVm extends Vm {
}
}
this.globals.uploadFile = (options, callback)=>{
this.checkContext('uploadFile')
if (typeof options === 'function') {
callback = options
options = {}
}
if (typeof callback === 'function') {
var widget = this.getWidget(),
valueOptions = this.getValueOptions()
uiFileUpload(options.types, (path, content)=>{
this.setWidget(widget)
this.setValueOptions(valueOptions)
try {
callback(content)
} catch(e) {
widget.errorProp('script', 'uploadFile', e)
}
this.setWidget()
this.setValueOptions()
}, (e)=>{
widget.errorProp('script', 'uploadFile', e)
})
} else {
widget.errorProp('script', 'browseFile', 'callback argument must be a function')
}
}
super.registerGlobals()
}