datasource.readFile ^v2.7.191
Reads data from a file in the MODLR file system. Supports multiple output formats and automatic character encoding detection.
js
datasource.readFile(filePath, readAs, encoding)Parameters
filePath- The name and file path of the file in the MODLR file system to read. File path can be found in the filesystem page.readAs(optional) - The format to return the file contents as. Defaults to"string". Supported values:"string"- Returns the entire file as a single string"lines"- Returns the file as an array of strings, one per line"json"- Parses the file as JSON and returns an object"xml"- Parses the file as XML and returns an object"base64"- Returns the file contents as a Base64 encoded string
encoding(optional) - The character encoding to use when reading the file. If omitted, empty, or set to"auto", the encoding is automatically detected. Otherwise, specify a valid charset name (e.g.,"UTF-8","ISO-8859-1").
Returns
- Returns the file contents in the format specified by
readAs, orfalseif the file path is invalid.
Example
js
// Read file as a string (default)
let text = datasource.readFile("input/report.txt");
// Read file as an array of lines
let lines = datasource.readFile("input/data.txt", "lines");
for (let line of lines) {
console.log(line);
}
// Read and parse a JSON file
let config = datasource.readFile("input/config.json", "json");
console.log(config.name);
// Read and parse an XML file
let xmlData = datasource.readFile("input/data.xml", "xml");
// Read a binary file as Base64
let base64 = datasource.readFile("input/image.png", "base64");
// Read a file with specific encoding
let legacyData = datasource.readFile("input/legacy.txt", "string", "ISO-8859-1");