Skip to content

filesrw.js

This page contains the full API reference for filesrw.js, which provides a file picker dialog for custom pages, and functions for reading and writing files on the server.

For how to use the file picker on a custom page, with examples, see Custom pages: dialogs, tabs, sliders and diagrams.

filesrw.js needs midas.js and controls.js to be included before it (see mhttpd.js). For security, all its functions can only access files in the userfiles subdirectory of the experiment directory, and its subdirectories. File names are relative to that directory, and can't contain ...

file_picker()

file_picker(pathName, ext, funcCall, saveFlag, param, fileTools, previewCall) opens a dialog in which the user chooses a file to load or save, and returns the dialog's element. It only chooses the file: your callback then reads or writes it, e.g. with file_load_ascii() or file_save_ascii().

Parameter Required Default Description
pathName ✔ Subdirectory of userfiles to start in, e.g. "equipment", or "" for userfiles itself. It is created if it doesn't exist
ext ✔ Files to show, e.g. "*.json", or "*.*" for all files
funcCall Function called as funcCall(filename, param) when the user clicks Load or Save (or double-clicks a file). filename is the path of the chosen file relative to userfiles, including the subdirectory
saveFlag false true for a save dialog, with a Save button and a box for typing a new file name. false for a load dialog
param {} Passed to funcCall and previewCall
fileTools false true shows buttons to create subdirectories, and rename and delete files and directories
previewCall Load dialogs only: adds a Preview button, which calls previewCall(filename, param) without closing the dialog

In the dialog, the user can:

  • Click a directory to open it, and use the path at the top to go back up
  • Sort the files by clicking the Name, Modified and Size column headings, and resize the columns by dragging their edges
  • Type the first letters of a file name to jump to it
  • With fileTools, select several entries with Ctrl-click (Cmd-click on macOS) or Shift-click, and delete them together. Directories must be empty to be deleted, and renaming never overwrites an existing entry

Reading and writing files

file_load_ascii()

file_load_ascii(filename, callback) reads a text file.

Parameter Required Description
filename ✔ Path of the file, relative to userfiles
callback ✔ Function called as callback(text) with the contents of the file

file_load_bin()

file_load_bin(filename, callback) reads a binary file.

Parameter Required Description
filename ✔ Path of the file, relative to userfiles
callback ✔ Function called as callback(bytes) with the contents of the file, as a Uint8Array

file_save_ascii()

file_save_ascii(filename, text, alert) writes a text file. If the file already exists, the user is asked whether to overwrite it.

Parameter Required Description
filename ✔ Path of the file, relative to userfiles
text ✔ Contents of the file
alert Message shown with dlgAlert() once the file has been saved

file_save_ascii_overwrite()

file_save_ascii_overwrite(filename, text, alert) writes a text file, overwriting it without asking if it already exists.

Parameter Required Description
filename ✔ Path of the file, relative to userfiles
text ✔ Contents of the file
alert Message shown with dlgAlert() once the file has been saved, or a function called as alert(filename)

These functions use the ext_list_files, ext_read_file, read_binary_file and ext_save_file JSON-RPC methods, which can also be called directly with mjsonrpc_call().

Functions and variables

Functions

applyHighlights

function applyHighlights(node, letters)

highlight characters in file selection table

Defined in resources/filesrw.js:753

check_sorting

function check_sorting(e)

Defined in resources/filesrw.js:978

clearHighlights

function clearHighlights()

clear highlighetd characters in file selction table

Defined in resources/filesrw.js:779

create_subdir

function create_subdir(subDir, pathName = "")

Handle create new folder

Defined in resources/filesrw.js:640

file_load_ascii

function file_load_ascii(filename, callback)

load an ascii file from server using mjsonrpc call

Defined in resources/filesrw.js:821

file_load_ascii_http

function file_load_ascii_http(file, callback)

load an ascii file from server using http request

Defined in resources/filesrw.js:809

file_load_bin

function file_load_bin(filename, callback)

load a binary file from server using mjsonrpc call

Defined in resources/filesrw.js:1092

file_picker

function file_picker(pathName, ext, funcCall, saveFlag = false, param = {}, fileTools = false, previewCall)

Load or save file from server, possible only restricted path

Defined in resources/filesrw.js:167

file_save_ascii

function file_save_ascii(filename, text, alert)

Save file to server

Defined in resources/filesrw.js:866

file_save_ascii_overwrite

function file_save_ascii_overwrite(filename, text, alert)

Defined in resources/filesrw.js:911

get_list_files

function get_list_files(files, subdirs)

get list of files and folders (in [])

Defined in resources/filesrw.js:788

resize_th

function resize_th()

Defined in resources/filesrw.js:1026

sort_files_select

function sort_files_select(sort)

Defined in resources/filesrw.js:931

table_file_dialog

function table_file_dialog(iddiv = "dlgFile", width = 600, height = 350, x, y)

Populates a modal with file load/save options in a table

Defined in resources/filesrw.js:517

tableClickHandler

function tableClickHandler()

Defined in resources/filesrw.js:675

unselect_files

function unselect_files()

Handle mouse click to unselect files

Defined in resources/filesrw.js:663

Variables

filesrw_css

var filesrw_css = …

Defined in resources/filesrw.js:76

fstyle const

const fstyle = document.createElement('style')

Defined in resources/filesrw.js:162

sequenceOfLetters

let sequenceOfLetters = ''

Handle key events in the files table created file_picker

Defined in resources/filesrw.js:673

sequenceTimer

let sequenceTimer = null

Defined in resources/filesrw.js:674