首页 > 解决方案 > session.will-download 事件触发一次

问题描述

我正在将电子与session.will-download事件一起使用,但遇到了问题。

我在 中嵌入了一个网络<webview>,并且网络内部有一个下载链接将下载 pdf 文件,我正在尝试下载具有特定目录的 pdf,它会触发will-download. 这很完美,但是当我点击更多次时,我发现它实际上不仅触发了一次。

mainWindow = new BrowserWindow({ width: 1100, height: 680 })
    mainWindow.loadURL(isDev ? 'http://localhost:3000' : `file://${path.join(__dirname, '../build/index.html')}`)
    mainWindow.on('closed', () => (mainWindow = null))

    mainWindow.webContents.on('did-attach-webview', (event, webContents) => {
        console.log('did-attach-webview emmit')
        webContents.on('new-window', (event, url) => {
            console.log(' ------ new-window .. -----')
            event.preventDefault()

            const win = new BrowserWindow({ show: false })
            win.once('ready-to-show', () => win.show())
            win.loadURL(url)
            event.newGuest = win

            const session = win.webContents.session
            session.on('will-download', (event, item, webContents) => {
                console.log(`${url} will-download ...`)
                const saveFileName = item.getFilename()
                item.setSavePath(`/Users/yang/Desktop/electron_practice/electron-react/src/${saveFileName}`)
            })
        })
    })

下面是我打印的日志

[1]  ------ new-window .. -----
[1] Blocked http://alp-rfb6-hjap01.alp.com.tw:30000/core/Default.html from calling focus on its opener.
[1] http://alp-rfb6-hjdb01/ReportServer?%2fB6_Waybill_C&rs:Command=Render&rs:Format=PDF&wh_id=RF-B6-2&order_number=DY01190503024 will-download ...
[1]  ------ new-window .. -----
[1] Blocked http://alp-rfb6-hjap01.alp.com.tw:30000/core/Default.html from calling focus on its opener.
[1] http://alp-rfb6-hjdb01/ReportServer?%2fB6_Waybill_C&rs:Command=Render&rs:Format=PDF&wh_id=RF-B6-2&order_number=DY01190503024 will-download ...
[1] http://alp-rfb6-hjdb01/ReportServer?%2fB6_Waybill_C&rs:Command=Render&rs:Format=PDF&wh_id=RF-B6-2&order_number=DY01190503024 will-download ...
[1]  ------ new-window .. -----
[1] Blocked http://alp-rfb6-hjap01.alp.com.tw:30000/core/Default.html from calling focus on its opener.
[1] http://alp-rfb6-hjdb01/ReportServer?%2fB6_Waybill_C&rs:Command=Render&rs:Format=PDF&wh_id=RF-B6-2&order_number=DY01190503024 will-download ...
[1] http://alp-rfb6-hjdb01/ReportServer?%2fB6_Waybill_C&rs:Command=Render&rs:Format=PDF&wh_id=RF-B6-2&order_number=DY01190503024 will-download ...
[1] http://alp-rfb6-hjdb01/ReportServer?%2fB6_Waybill_C&rs:Command=Render&rs:Format=PDF&wh_id=RF-B6-2&order_number=DY01190503024 will-download ...
[1]  ------ new-window .. -----
[1] Blocked http://alp-rfb6-hjap01.alp.com.tw:30000/core/Default.html from calling focus on its opener.
[1] http://alp-rfb6-hjdb01/ReportServer?%2fB6_Waybill_C&rs:Command=Render&rs:Format=PDF&wh_id=RF-B6-2&order_number=DY01190503024 will-download ...
[1] http://alp-rfb6-hjdb01/ReportServer?%2fB6_Waybill_C&rs:Command=Render&rs:Format=PDF&wh_id=RF-B6-2&order_number=DY01190503024 will-download ...
[1] http://alp-rfb6-hjdb01/ReportServer?%2fB6_Waybill_C&rs:Command=Render&rs:Format=PDF&wh_id=RF-B6-2&order_number=DY01190503024 will-download ...
[1] http://alp-rfb6-hjdb01/ReportServer?%2fB6_Waybill_C&rs:Command=Render&rs:Format=PDF&wh_id=RF-B6-2&order_number=DY01190503024 will-download ...

我猜这是会话上出现的问题,应该在下载完成后关闭它,或者其他什么。有没有人也遇到这个问题:(

标签: javascriptelectron

解决方案


下载完成后尝试关闭窗口,注册多个会话将下载处理程序的问题。下载后关闭窗口可以帮助我想。请尝试让我知道。

mainWindow = new BrowserWindow({ width: 1100, height: 680 })
    mainWindow.loadURL(isDev ? 'http://localhost:3000' : `file://${path.join(__dirname, '../build/index.html')}`)
    mainWindow.on('closed', () => (mainWindow = null))

    mainWindow.webContents.on('did-attach-webview', (event, webContents) => {
        console.log('did-attach-webview emmit')
        webContents.on('new-window', (event, url) => {
            console.log(' ------ new-window .. -----')
            event.preventDefault()

            const win = new BrowserWindow({ show: false })
            win.once('ready-to-show', () => win.show())
            win.loadURL(url)
            event.newGuest = win

            const session = win.webContents.session
            session.on('will-download', (event, item, webContents) => {
                console.log(`${url} will-download ...`)
                const saveFileName = item.getFilename()
                item.setSavePath(`/Users/yang/Desktop/electron_practice/electron-react/src/${saveFileName}`)

            item.once('done', (event, state) => {
             if (state === 'completed') {
              win.close();
             } else {
              win.close();
             }
            })
            })
        })
    })

推荐阅读