Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
C
course_creation
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
蔡芬
course_creation
Commits
fdda23a4
提交
fdda23a4
authored
11月 25, 2021
作者:
蔡芬
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
主和渲染进程 交互
上级
e5c4bccf
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
100 行增加
和
15 行删除
+100
-15
background.js
src/background.js
+55
-7
fullpage.vue
src/components/scaffold/fullpage.vue
+35
-1
backfunction.js
src/libs/utils/backfunction.js
+10
-7
没有找到文件。
src/background.js
浏览文件 @
fdda23a4
...
...
@@ -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"
,
()
=>
{
...
...
src/components/scaffold/fullpage.vue
浏览文件 @
fdda23a4
...
...
@@ -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
)
})
},
},
}
...
...
src/libs/utils/backfunction.js
浏览文件 @
fdda23a4
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
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论