跳转到主内容

webUtils

一个与Web API 对象交互的工具层 (Files、 Blobs 等)

Process: Renderer

方法

webUtils模块有以下方法:

webUtils.getPathForFile(file)

  • file File - Web File 对象.

返回 string - 此 File 对象指向的文件系统路径。 如果转入的对象不是一个 File 对象,则会抛出异常。 如果传入的文件对象是在 JS中构建的并且不在在磁盘上的文件存在,则返回一个空字符串。

This method superseded the previous augmentation to the File object with the path property. 下面是一个例子。

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