webUtils
Web API オブジェクト (File、Blob など) とやり取りするためのユーティリティ層
プロセス: レンダラー
情報
コンテキスト分離が有効になっているレンダラープロセスからこの API を呼び出す場合は、プリロードスクリプトに API 呼び出しを記述し、contextBridge API を使用して 公開 してください。
メソッド
webUtils モジュールには以下のメソッドがあります。
webUtils.getPathForFile(file)
fileFile - ウェブの 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.
}
})