electron/docs/api/web-utils.md
trop[bot] 347fd37dfb
docs: mention that webUtils should be used via preload script (#48454)
docs: mention that webUtils should be used via preload script (#45861)

* docs: mention that webUtils should be used via preload script

* docs: suppress lint errors

* docs: clarify webUtils usage scope

* docs: exclude potentially dangerous alert() in the example code

* docs: minor change

* docs: minor change

* docs: minor change

* docs: minor change

* docs: minor change

* docs: minor change

* docs: minor change

* docs: minor change

* docs: minor change

* docs: minor change

* docs: make linter happy

* docs: apply suggestion



* docs: apply suggestion



* docs: apply suggestion



* docs: minor change

* docs: minor change

* docs: remove preload line

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Kaiichiro Ota <kigh.ota@gmail.com>
2025-10-03 15:48:06 -05:00

1.4 KiB

webUtils

A utility layer to interact with Web API objects (Files, Blobs, etc.)

Process: Renderer

Methods

The webUtils module has the following methods:

webUtils.getPathForFile(file)

  • file File - A web File object.

Returns string - The file system path that this File object points to. In the case where the object passed in is not a File object an exception is thrown. In the case where the File object passed in was constructed in JS and is not backed by a file on disk an empty string is returned.

This method superseded the previous augmentation to the File object with the path property. An example is included below.

// Before (renderer)
const oldPath = document.querySelector('input[type=file]').files[0].path
// After

// Renderer:

const file = document.querySelector('input[type=file]').files[0]
electronApi.doSomethingWithFile(file)

// Preload script:

const { contextBridge, webUtils } = require('electron')

contextBridge.exposeInMainWorld('electronApi', {
  doSomethingWithFile (file) {
    const path = webUtils.getPathForFile(file)
    // Do something with the path, e.g., send it over IPC to the main process.
    // It's best not to expose the full file path to the web content if possible.
  }
})