javascript - Electron + Angular = 没有本地文件?

标签 javascript angular express electron

我的问题很难解释,但我会尝试 - 尝试从应用程序目录播放 mp3 文件时,出现 Electron 错误不允许加载本地资源

发生这种情况是因为我使用 Webpack 进行开发,使用 Express 服务器进行生产来运行我的应用程序(由于某些 Angular 路由功能而使用 Express)。

这是我的 electron-main.js 的一部分 -

function createWindow() {
    win = new BrowserWindow({
    width: 1500,
    height: 900,
    resizable: true,
    center: true,
    autoHideMenuBar: true,
    icon: path.join(__dirname, 'static', 'note.ico')
  });

   if (DEBUG) {
     // Loading from Webpack
     win.loadURL(`http://localhost:4200`);
     win.webContents.openDevTools();
   }
   else {
     // Loading from Express-server
     const server = require("../server");
     win.loadURL(`http://localhost:3333`);
   }

服务器.js

let path = require('path');
let express = require('express');
let app = express();

app.use(express.static(path.join(__dirname, 'dist')));

app.get('/', function (req, res) {
  res.sendFile(path.join(__dirname, 'dist', 'index.html'));
});

app.listen(3333);

但是当我尝试设置时

win.loadURL(url.format({
    pathname: path.join(__dirname, 'index.html'),
    protocol: 'file:',
    slashes: true
  }));

我已经播放了 mp3

所以我明白只有当 protocol: 'file:' 时才能访问本地文件,而不是 http: 应用程序结构应该相似 -

(root folder)
--app.exe (whatever)
--mp3
  --1.mp3
  --2.mp3
  --3.mp3
  ...

有解决办法吗?也许我可以使用 Express 来获取我的文件?

最佳答案

我发现的唯一解决方法是使用 file: 协议(protocol)创建额外的隐藏窗口,如下所示

hiddenWindow = new BrowserWindow({
  show: false
});

hiddenWindow.loadURL(url.format({
  pathname: path.join(__dirname, 'hidden.html'),
  protocol: 'file:',
  slashes: true
}));

并使用ipcRendereripcMain初始化它的函数。

关于javascript - Electron + Angular = 没有本地文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43683299/

相关文章:

javascript - Angular : strange behavior with ngCookies

javascript - SourceForge 的下载页面如何工作?

javascript - Sequelize 自引用 hasMany 不创建别名函数

javascript - 链接上的 jQuery Datatable javascript 不适用于多个页面

javascript - 使用 Vanilla-JS 或 Angular 输入多种颜色

Angular 6 - 为什么生产版本中缺少 Bearer Token? (在开发构建中工作正常)

node.js - 如何动态渲染表达 View

angular - RXJS 在管道内组合多个可观察对象

javascript - 我正在尝试在程序中使用 Mongoose 模式,但它不断显示错误用户不是构造函数

javascript - Node.js redis pub/sub 丢失消息