Skip to main content

File System

Cpp.js uses a virtual file system in the browser. To easily interact with this virtual file system, a set of helper JavaScript functions is provided. These functions:

FunctionPlatformInputOutputDescription
generateVirtualPathbrowserCreates a directory at /virtual/automounted/{{RANDOM}}
autoMountFilesbrowserFileList | [File][string]Auto mount files.
getFileBytesbrowser, node.jsstringUint8ArrayReturns the file at the specified path in bytes.
getFileListbrowser, node.jsstring[{ path: string, size: number }]Returns the files with path and size at the specified path.

Examples

Opening file from file input.

// HTML
<input class="input-file" type="file" name="file" id="file" onChange="onFileChange" />
// JS
function onFileChange({ target }) {
const files = Module.autoMountFiles([target.file]);
}

Opening files from file input. (multiple)

// HTML
<input class="input-file" type="file" name="files[]" id="file" onChange="onFileChange" multiple />
// JS
function onFileChange({ target }) {
const result = Module.autoMountFiles(target.files);
}

Opening a file from the network.

const fileData = await fetch('test/polygon.geojson');
const file = new File([await fileData.blob()], "polygon.geojson");
const result = Module.autoMountFiles([file]);

Download file from "/output" path on the browser.

const filePath = '/virtual/test.json';
const fileBytes = Module.getFileBytes(filePath);
const fileName = filePath.split('/').pop();
saveAs(fileBytes, filename);

function saveAs(fileBytes, fileName) {
const blob = new Blob([fileBytes]);
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = fileName;
link.click();
}

Displays information about the files.

const files = Module.getFileList();
files.forEach((fileInfo) => {
console.log(`file path: ${fileInfo.path}, file size: ${fileInfo.size}`);
});

info

Browser Functions: You can access the browser functions from this link.
Node.js Functions: You can access the Node.js functions from this link.