Electron - 使用 showOpenDialog 时抛出不允许加载本地资源

2023-11-22

我只是想用showOpenDialog并加载图像。但是当我选择图像时应用程序会崩溃。

main.js:

...
  ipcMain.on('open-file-dialog', function (event) {
    const window = BrowserWindow.getFocusedWindow();

    dialog.showOpenDialog(window, {
      properties: ['openFile']
    }, p => {
      console.log(p)
    });
  })

渲染器.js:

document.querySelector('#select-image').addEventListener('click', function (event) {
    ipcRenderer.send('open-file-dialog')
});

当我选择任何内容时,此错误会在控制台中显示:Not allowed to load local resource: file:///startElectron 版本是8.2.5

Edit 1:

此警告(或可能是错误)显示在终端中objc[50899]: Class FIFinderSyncExtensionHost is implemented in both /System/Library/PrivateFrameworks/FinderKit.framework/Versions/A/FinderKit (0x7fff951e61d0) and /System/Library/PrivateFrameworks/FileProvider.framework/OverrideBundles/FinderSyncCollaborationFileProviderOverride.bundle/Contents/MacOS/FinderSyncCollaborationFileProviderOverride (0x11298bdc8). One of the two will be used. Which one is undefined.

编辑2: 我使用 Electron Fiddle 将示例要点放在一起:here


Electron 不允许 windowswebSecurity: true加载本地文件。

糟糕的解决方案

消除错误的一种方法是将其简单地设置为false。但这会使您的应用程序使用起来不太安全:

new BrowserWindow({
  ...
  webPreferences: {
    webSecurity: false
  }
})

好的解决方案

相反,您需要做的是创建一个自定义协议,然后使用它来加载文件。

第 1 步:创建自定义协议

主要流程:

import { protocol } from 'electron'

...

app.on('ready', async () => {
  // Name the protocol whatever you want.
  const protocolName = 'your-app-name'

  protocol.registerFileProtocol(protocolName, (request, callback) => {
    const url = request.url.replace(`${protocolName}://`, '')
    try {
      return callback(decodeURIComponent(url))
    }
    catch (error) {
      // Handle the error as needed
      console.error(error)
    }
  })
  ...

Note:我不确定协议名称是否必须是唯一的,我从未测试过。如果您只是将协议命名为safe-protocol并且您的应用程序的用户有另一个已注册的应用程序 Xsafe-protocol,您的应用程序要么在打开时抛出错误,要么会注册这两个应用程序,但当用户尝试从 URL 打开其应用程序时,使用app.setAsDefaultProtocolClient功能,这两个应用程序都会打开。不确定在这种情况下会发生什么。

第二步:使用协议加载文件

方法一:从主进程获取路径:

主要流程:

ipcMain.on('open-file-dialog', function (event) {
  const window = BrowserWindow.getFocusedWindow();

  dialog.showOpenDialog(window, { properties: ['openFile'] })
    .then(result => {
      // Send the path back to the renderer
      event.sender.send('open-file-dialog-reply', { path: result.filePaths[0] })
    })
    .catch(error => {
       console.log('Could not get file path')
    })
})

渲染器进程:

<img id="image-1">
ipcRenderer.on('open-file-dialog-reply', (event, data) => {
  const path = data.path
  loadImage(path)
}

function loadImage (path) {
  const image1 = document.getElementById('image-1')
  image1.src = `safe-file-protocol://${path}`
}

方法2:直接在渲染器中加载路径:

渲染器进程:

<img id="image-1" data-path="C:/test.jpg">
function loadImage () {
  const image1 = document.getElementById('image-1')
  const path = image1.dataset.path
  image1.src = `safe-file-protocol://${path}`
}

loadImage()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Electron - 使用 showOpenDialog 时抛出不允许加载本地资源 的相关文章

随机推荐