メインコンテンツへ飛ぶ

webUtils

Web API オブジェクト (File、Blob など) とやり取りするためのユーティリティ層

Process: Renderer

info

If you want to call this API from a renderer process with context isolation enabled, place the API call in your preload script and expose it using the contextBridge API.

メソッド

webUtils モジュールには以下のメソッドがあります。

webUtils.getPathForFile(file)

  • file File - ウェブの File オブジェクト。

戻り値 string - この File オブジェクトが指すファイルシステムのパス。 渡されたオブジェクトが File オブジェクトでない場合、例外が送出されます。 渡された File オブジェクトが JS 内で構築され、ディスク上のファイルに由来しない場合、空の文字列が返されます。

このメソッドは、以前の path プロパティを付けた File オブジェクト拡張の置き換えとして用意されました。 以下にサンプルを示します。

// 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.
}
})