Observable Framework 1.7.0 GitHub️ 1.9k

File input

API · Source · The file input specifies a local file and is intended for prompting the user to select a file from their own machine. The exposed value provides the same interface as FileAttachment for convenient parsing in various formats such as text, image, JSON, CSV, ZIP, and XLSX.

By default, any file is allowed, and the value of the input resolves to null.

const file = view(Inputs.file());
file

We recommend providing a label to improve usability.

Specify the accept option to limit the allowed file extensions. This is useful when you intend to parse the file as a specific file format, such as CSV. By setting the required option to true, the value of the input won’t resolve until the user choses a file.

const csvfile = view(Inputs.file({label: "CSV file", accept: ".csv", required: true}));

Once a file has been selected, you can read its contents like so:

csvfile.csv({typed: true})

Here are examples of other supported file types.

const jsonfile = view(Inputs.file({label: "JSON file", accept: ".json", required: true}));
jsonfile.json()
const textfile = view(Inputs.file({label: "Text file", accept: ".txt", required: true}));
textfile.text()
const imgfile = view(Inputs.file({label: "Image file", accept: ".png,.jpg", required: true}));
imgfile.image()
const xlsxfile = view(Inputs.file({label: "Excel file", accept: ".xlsx", required: true}));
xlsxfile.xlsx()
const zipfile = view(Inputs.file({label: "ZIP archive", accept: ".zip", required: true}));
zipfile.zip()

The multiple option allows the user to pick multiple files. In this mode, the exposed value is an array of files instead of a single file.

const files = view(Inputs.file({label: "Files", multiple: true}));
files

Options

Inputs.file(options)

The available file input options are:

The value of file input cannot be set programmatically; it can only be changed by the user.