提交 fdda23a4 authored 作者: 蔡芬's avatar 蔡芬

主和渲染进程 交互

上级 e5c4bccf
......@@ -34,13 +34,18 @@ async function createWindow() {
// frame: false, //无边框窗口
webPreferences: {
// Required for Spectron testing
enableRemoteModule: !!process.env.IS_TEST,
// enableRemoteModule: !!process.env.IS_TEST,
// 在electron 10.0.0之后,remote模块默认关闭
// 必须手动设置webPreferences中的enableRemoteModule为true之后才能使用
enableRemoteModule: true, // 这里是关键设置
// Use pluginOptions.nodeIntegration, leave this alone
// See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
// nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION,
nodeIntegration: true, //web开启主进程中集成node
// contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION,
nodeIntegration: true, //web开启主进程中集成node
contextIsolation: false,
preload: path.join(__dirname, 'preload.js')
},
......@@ -90,16 +95,59 @@ async function createWindow() {
// });
// // if (!process.env.IS_TEST) a.webContents.openDevTools()
// })
var downloadpath = '';
var folderpath = '';
win.webContents.session.on('will-download', (event, item, webContents) => {
//设置文件存放位置
// console.log(folderpath,item.getFilename(),folderpath+`\\${item.getFilename()}`,'C:/Users/mayn/Downloads/test/'+`${item.getFilename()}`)
item.setSavePath(folderpath+`\\${item.getFilename()}`);
// item.setSavePath('C:/Users/mayn/Downloads/test/'+`${item.getFilename()}`);
item.on('updated', (event, state) => {
console.log(`Received bytes: ${item.getReceivedBytes()}`)
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')
} else {
console.log(`Download failed: ${state}`)
}
})
});
ipcMain.on('downLoadFile', (evt,{downLoadUrl,targetUrl}) => {
targetUrl = targetUrl.replace(/\\/g, '/');
targetUrl = targetUrl + '/' +downLoadUrl.substring(downLoadUrl.lastIndexOf('/')+1);
console.log('主进程监听到下载',downLoadUrl,targetUrl);
downloadFile(downLoadUrl, targetUrl,({percentage,received,total,success})=>{
if(!!success) {
console.log('文件下载成功');
return;
}
console.log(percentage + "% | " + received + " bytes out of " + total + " bytes.");
});
// electron 的下载一直 interrupted -- 失败
// downloadpath = downLoadUrl;
// folderpath = targetUrl;
// evt.sender.send('tips',downloadpath);
// win.webContents.downloadURL(downloadpath);
});
}
ipcMain.on('fromFullPage', e => {
console.log('主进程监听到渲染进程');
});
ipcMain.on('downLoadFile', (e,arg) => {
console.log('主进程监听到下载',arg);
console.log(downloadFile)
// downloadFile(arg,'E:/360Downloads/test.zip')
});
// Quit when all windows are closed.
app.on("window-all-closed", () => {
......
......@@ -138,7 +138,41 @@ export default {
this.$electron && this.$electron.shell.openExternal("https://www.baidu.com"); //electron 打开默认浏览器访问
},
downFile() {
this.$electron && this.$electron.ipcRenderer.send('downLoadFile','https://revolutionary.jkxuetang.com/download?path=C6EDPYre%2Bvu%2B10Ub4uYjYZEPJYjKE46z4wma6JUAOsvvm2Ki4ypAeInpzQXufiOx0ObQDs4usDE8WBNJ51cepu0IOqQ02vOCnT4H03JX0Y45hFe2GGOhuj%2F%2Fog0AkB8q3kMKNvuohAzcMeY0JI%2FYSV0a59JlYJqJ28cGJsE6r%2FfBeWI&name=C6tQONWE%2FPyixkIfoew9&corpid=B%2FNWbYuG9fm1zEMb%2FutiPb8xYJXxS8uusDKF6g&t=D%2FdXbtbfqf7nlQJcpug7fstM');
// let downLoadUrl = 'https://builder.fei-dao.com/download/615041cbb0b3b518f8369289/61663ca685c24474ab2211cb/444/download.zip';
// let downLoadUrl = 'https://mp32.9ku.com/upload/128/2020/04/17/1003659.mp3';
// let downLoadUrl = 'https://jkxuetang-master.oss-cn-shenzhen.aliyuncs.com/tmp/msg.png';
let downLoadUrl = 'https://jkxuetang-master.oss-cn-shenzhen.aliyuncs.com/tmp/test001%20(1).zip';
// 桌面端
this.desktopDownLoad(downLoadUrl);
// 浏览器
if(!this.$electron) {
this.webDownLoad(downLoadUrl);
}
},
webDownLoad(downLoadUrl) {
window.location.href = downLoadUrl;
},
desktopDownLoad(downLoadUrl) {
this.$electron && this.$electron.remote && this.$electron.remote.dialog.showOpenDialog(null, {
// 默认文件夹
// defaultPath :'../Downloads',
//选择操作,此处是打开文件夹
properties: ['openDirectory'],
filters: [
{ name: 'All', extensions: ['*'] },
]
}).then(result => {
console.log(result.canceled)
console.log(result.filePaths)
//取消了选择
if(!!result.canceled) {
return;
}
this.$electron && this.$electron.ipcRenderer.send('downLoadFile', {downLoadUrl: downLoadUrl, targetUrl: result.filePaths[0]});
}).catch(err => {
console.log(err)
})
},
},
}
......
var request = require('request');
var fs = require('fs');
export const downloadFile = function(file_url , targetPath) {
export const downloadFile = function(file_url , targetPath, callback) {
// Save variable to know progress
var received_bytes = 0;
var total_bytes = 0;
......@@ -14,6 +14,7 @@ export const downloadFile = function(file_url , targetPath) {
req.pipe(out);
req.on('response', function ( data ) {
console.log('Response',data)
// Change the total bytes value to get progress later.
total_bytes = parseInt(data.headers['content-length' ]);
});
......@@ -22,14 +23,16 @@ export const downloadFile = function(file_url , targetPath) {
// Update the received bytes
received_bytes += chunk.length;
showProgress(received_bytes, total_bytes);
showProgress(received_bytes, total_bytes, callback);
});
req.on('end', function() {
alert("File succesfully downloaded");
console.log("File succesfully downloaded");
callback({success:true});
});
}
export const showProgress = function(received, total){
var percentage = (received * 100) / total;
console.log(percentage + "% | " + received + " bytes out of " + total + " bytes.");
}
export const showProgress = function(received, total, callback){
var percentage = (received * 100) / total;
// console.log(percentage + "% | " + received + " bytes out of " + total + " bytes.");
callback({percentage,received,total});
}
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论