electron img无法加载 带file:///的绝对路径图片问题

原因electron 4.x开始后的安全策略,不识别file:///开始的url

解决方案:main.js中,加入webSecurity: false,并且自定义file:///协议的解析

import { app, BrowserWindow, ipcMain, protocol } from 'electron'
function createWindow () {
  /**
   * Initial window options
   */
  mainWindow = new BrowserWindow({
    height: 768,
    useContentSize: true,
    width: 1300,
    webPreferences:{
      webSecurity: false, //========关闭安全策略===========
      nodeIntegration: true,
    }
  })
 
  //===========自定义file:///协议的解析=======================
  protocol.interceptFileProtocol('file', (req, callback) => {
    const url = req.url.substr(8);
    callback(decodeURI(url));
  }, (error) => {
    if (error) {
      console.error('Failed to register protocol');
    }
  });
}

在new window后,注册file:///协议,自己识别该协议

原文链接:https://blog.csdn.net/youyudexiaowangzi/article/details/113935045

你可能感兴趣的:(vue,javascript)