如何使Electron在外部应用程序中自动打开下载的文件?

时间:2019-05-22 11:35:19

标签: javascript pdf electron docx

我正在用HTML和Javascript构建一个Electron应用程序。我希望该应用程序打开下载的文件,例如PDF,DOCX等在外部标准应用程序(如Adobe Reader和Word)中自动运行。是否有一个简单的Javascript函数来实现这一目标,或者是一种更好的方法?现在,Electron会打开下载对话框,就像在Chrome中一样。不幸的是,我没有太多的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)

  }

1 个答案:

答案 0 :(得分:1)

您可以使用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/download-item