2023-11-20 15:59:36 -08:00
# webUtils
> A utility layer to interact with Web API objects (Files, Blobs, etc.)
Process: [Renderer ](../glossary.md#renderer-process )
## Methods
The `webUtils` module has the following methods:
### `webUtils.getPathForFile(file)`
* `file` File - A web [File ](https://developer.mozilla.org/en-US/docs/Web/API/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.
2024-08-22 15:44:55 +02:00
This method superseded the previous augmentation to the `File` object with the `path` property. An example is included below.
2023-11-20 15:59:36 -08:00
2025-03-21 08:40:49 +01:00
```js @ts -nocheck
2025-10-03 15:48:06 -05:00
// Before (renderer)
const oldPath = document.querySelector('input[type=file]').files[0].path
```
2023-11-20 15:59:36 -08:00
2025-10-03 15:48:06 -05:00
```js @ts -nocheck
2023-11-20 15:59:36 -08:00
// After
2025-05-29 12:45:26 -07:00
2025-10-03 15:48:06 -05:00
// 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.
}
})
2023-11-20 15:59:36 -08:00
```