[英]How to make Electron automatically open downloaded files in external application?
I am building an Electron app with HTML and Javascript. 我正在用HTML和Javascript构建Electron应用程序。 I want the app to open downloaded files eg PDFs, DOCXs etc. automatically in the external standard application like Adobe Reader and Word.
我希望该应用程序在Adobe Reader和Word等外部标准应用程序中自动打开下载的文件,例如PDF,DOCX等。 Is there a simple Javascript function to achieve this or maybe a better way?
是否有一个简单的Javascript函数可以实现这一目标,或者有更好的方法? Right now Electron opens the download dialog, like in Chrome.
现在,Electron会打开下载对话框,就像在Chrome中一样。 I unfortunately don't have a lot of experience with Javascript, so I apologize if this is a too simple question for you to pay attention to.
不幸的是,我没有太多的Java经验,因此我很抱歉这是一个太简单的问题,您无需注意。
const electron = require ('electron');
const url = require('url');
const path = require('path');
// In the main process.
const { app, Menu, BrowserWindow , shell } = require('electron')
// Listen for the app to be ready
app.on('ready', function() {
// Create new window
mainWindow = new BrowserWindow({});
// Load html into window
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}));
// Build menu from template
const mainMenu = Menu.buildFromTemplate(mainMenuTemplate);
// Insert menu
Menu.setApplicationMenu(mainMenu);
mainWindow.webContents.session.on('will-download', (event, item, webContents) => {
// Set the save path, making Electron not to prompt a save dialog.
item.setSavePath('/tmp/save.pdf')// get the filename from item object and provide here according to your logic
item.on('updated', (event, state) => {
if (state === 'interrupted') {
console.log('Download is interrupted but can be resumed')
} else if (state === 'progressing') {
if (item.isPaused()) {
console.log('Download is paused')
} else {
console.log(`Received bytes: ${item.getReceivedBytes()}`)
}
}
})
item.once('done', (event, state) => {
if (state === 'completed') {
console.log('Download successfully')
//Open the document using the external application
shell.openItem(item.getSavePath());
} else {
console.log(`Download failed: ${state}`)
}
})
})
});
// Create menu template
const mainMenuTemplate = [
{
label: 'File',
submenu: [
{
label: 'Quit',
accelerator: process.platform == 'darwin' ? 'Command+Q' : 'Ctrl+Q',
click(){
app.quit();
}
}
],
},
{
label: 'View',
submenu: [
{
label: 'Toggle Fullscreen',
click(){
mainWindow.isFullScreen() == true ? mainWindow.setFullScreen(false) : mainWindow.setFullScreen(true)
}
}
],
}
];
function toggleFullScreen() {
mainWindow.setFullScreen(true) ? mainWindow.setFullScreen(false) : mainWindow.setFullScreen(true)
}
You can intercept the downloads using will-download event and show the downloaded file using shell.openItem(); 您可以使用will-download事件拦截下载,并使用shell.openItem();显示下载的文件。
// In the main process.
const {app, BrowserWindow, Menu ,shell } = electron;
app.on('ready', function() {
// Create new window
mainWindow = new BrowserWindow({});
// Load html into window
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}));
// Build menu from template
const mainMenu = Menu.buildFromTemplate(mainMenuTemplate);
// Insert menu
Menu.setApplicationMenu(mainMenu);
mainWindow.webContents.session.on('will-download', (event, item, webContents) => {
// Set the save path, making Electron not to prompt a save dialog.
item.setSavePath('/tmp/save.pdf')// get the filename from item object and provide here according to your loic
item.on('updated', (event, state) => {
if (state === 'interrupted') {
console.log('Download is interrupted but can be resumed')
} else if (state === 'progressing') {
if (item.isPaused()) {
console.log('Download is paused')
} else {
console.log(`Received bytes: ${item.getReceivedBytes()}`)
}
}
})
item.once('done', (event, state) => {
if (state === 'completed') {
console.log('Download successfully')
//Open the document using the external application
shell.openItem(item.getSavePath());
} else {
console.log(`Download failed: ${state}`)
}
})
})
});
https://electronjs.org/docs/api/shell https://electronjs.org/docs/api/shell
https://electronjs.org/docs/api/download-item https://electronjs.org/docs/api/download-item
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.