Back to Content

FileSystemFileHandle: getFile() method

files/en-us/web/api/filesystemfilehandle/getfile/index.md

latest1.5 KB
Original Source

{{securecontext_header}}{{APIRef("File System API")}}{{AvailableInWorkers}}

The getFile() method of the {{domxref("FileSystemFileHandle")}} interface returns a {{jsxref('Promise')}} which resolves to a {{domxref('File')}} object representing the state on disk of the entry represented by the handle.

If the file on disk changes or is removed after this method is called, the returned {{domxref('File')}} object will likely be no longer readable.

Syntax

js-nolint
getFile()

Parameters

None.

Return value

A {{jsxref('Promise')}} which resolves to a {{domxref('File')}} object.

Exceptions

  • NotAllowedError {{domxref("DOMException")}}
    • : Thrown if the {{domxref('PermissionStatus.state')}} is not granted in read mode.
  • NotFoundError {{domxref("DOMException")}}
    • : Thrown if current entry is not found.

Examples

The following asynchronous function presents a file picker and once a file is chosen, uses the getFile() method to retrieve the contents.

js
async function getTheFile() {
  // open file picker
  const [fileHandle] = await window.showOpenFilePicker(pickerOpts);

  // get file contents
  const fileData = await fileHandle.getFile();
}

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}

See also