docs.rodeo

MDN Web Docs mirror

File API

{{DefaultAPISidebar("File API")}} {{AvailableInWorkers}} 

Concepts and Usage

The File API enables web applications to access files and their contents.

Web applications can access files when the user makes them available, either using a file <input> element or via drag and drop.

Sets of files made available in this way are represented as {{domxref("FileList")}}  objects, which enable a web application to retrieve individual {{domxref("File")}}  objects. In turn {{domxref("File")}}  objects provide access to metadata such as the file’s name, size, type, and last modified date.

{{domxref("File")}}  objects can be passed to {{domxref("FileReader")}}  objects to access the contents of the file. The {{domxref("FileReader")}}  interface is asynchronous, but a synchronous version, available only in web workers, is provided by the {{domxref("FileReaderSync")}}  interface.

There are two other major APIs that also deal with files: File and Directory Entries API and File System API.

The File API is the most basic one. It supports reading and processing file data explicitly provided by the user in the form of a form element input or drag-and-drop operation. It also enables binary data handling via blobs.

The File and Directory Entries API, like the File API, also deals with files provided by the user via form inputs or drag-and-drop operations. However, instead of a single file, the input element now allows the selection of a directory or multiple files. The API then provides a way to process the directory or files. It is mostly Chrome’s own invention—you will find that its extensions to other interfaces are all prefixed with webkit. The File and Directory Entries API has a more complete story about its implementation and standardization. It was originally intended to support a full virtual file system, but now only supports read operations on user-provided data.

The File System API provides a virtual file system for web applications, so that they can persistently store data in a virtual system which is private to the document’s origin (known as an Origin private file system (OPFS)). The File System Access API further extends the File System API to allow websites to read and write user files, subject to user consent. Unlike the File API and the File and Directory Entries API, the File System API is purely JavaScript and does not deal with form inputs.

Interfaces

Extensions to other interfaces

Examples

Reading a file

In this example, we provide a file <input> element, and when the user selects a file, we read the contents of the first file selected as text, and display the result in a {{HTMLElement("div")}} .

HTML

<input type="file" />
<div class="output"></div>

CSS

.output {
  overflow: scroll;
  margin: 1rem 0;
  height: 200px;
}

JavaScript

const fileInput = document.querySelector("input[type=file]");
const output = document.querySelector(".output");

fileInput.addEventListener("change", async () => {
  const [file] = fileInput.files;

  if (file) {
    output.innerText = await file.text();
  }
});

Result

{{EmbedLiveSample("Reading a file", "", "300")}} 

Specifications

{{Specifications}} 

See also

In this article

View on MDN