electron-builder 打包后 出现Not allowed to load local resource:
时间: 2023-12-15 08:31:46 浏览: 79
electron-builder打包后出现"Not allowed to load local resource"错误通常是因为应用程序试图加载本地文件,但由于安全原因,Electron不允许这样做。以下是两种可能的解决方法:
1.使用electron-builder的extraResources选项将本地文件添加到应用程序中。在package.json文件中添加以下内容:
```json
"build": {
"extraResources": [
"path/to/local/file"
]
}
```
2.使用electron的remote模块从主进程中访问本地文件。在主进程中添加以下代码:
```javascript
const { app, BrowserWindow } = require('electron')
const path = require('path')
function createWindow () {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true // 添加这一行
}
})
win.loadFile('index.html')
}
app.whenReady().then(() => {
createWindow()
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
// 在主进程中访问本地文件
const fs = require('fs')
const filePath = path.join(__dirname, 'path/to/local/file')
fs.readFile(filePath, (err, data) => {
if (err) {
console.error(err)
return
}
console.log(data)
})
```