Electron下载插件 or 固件至本地

最近有个特殊需求,由于路由器PCB板子是办公类型,无线传输类型,在特定情况下没有网络支持。则桌面应用程序里面的远程升级路由器功能无法使用,则就需要将OTA升级降至本地通过局域网的HTTP请求去实现。

1.下载代码

javascript 复制代码
        Downloads(url){
                var that = this;
                var http = require('http');
                var fs = window.require('fs');
                http.get(url, (res) => { 
					var localFirmwareUrl = process.env.NODE_ENV == "development"? __dirname.split("node_modules")[0]+"build/NewFirmware":__dirname.split("Tools")[0]+"Services/Services";
					if (res.statusCode !== "200") {
						const file = fs.createWriteStream(localFirmwareUrl)
						// 进度
						const len = parseInt(res.headers['content-length']);// 文件总长度
						let cur = 0;
						res.on('data', function (chunk) {
							cur += chunk.length;
							const progress = (100.0 * cur / len).toFixed(2);// 当前进度
							const currProgress = (cur / 1048576).toFixed(2);// 当前了多少
							// console.log(progress);
							// console.log(currProgress + "M");
							that.Download.len = progress/5;
							document.getElementById("restart_step").style.cssText += "width: " + progress/5 + "%;";
						})
						//下载结束
						res.on('end', () => {
							
						})
						//文件写入结束
						file.on('finish', () => {
							file.close();
                            that.File_Upload();
						}).on('error', (err) => {
							fs.unlink(localFirmwareUrl,(e)=>{console.log(e)});
							if (err) {
								console.log(err);
							}
						})
						res.pipe(file);
					} else {
						console.log("网络环境错误!");
						ipcRenderer.sendSync('message', JSON.stringify({
							command: "notification",
							title: that.$t("notification.title_2"),
							body: that.$t("notification.body_27")
						}));
					}
			    })
            },

2.将文件封装成formData

javascript 复制代码
BufConvertFile(url, filename) {
	var fs = window.require('fs');
	console.log(url)
	return new Promise((resolv, reject) => {
		fs.readFile(url, (err, data) => {
			if (err) {
				return
			}
			const blob = new Blob([data], { type: 'text/plain' });
			const file = new File([blob], filename, { type: 'text/plain' });
			resolv(file);
		})
	})
}


File_Upload(){
    var { BufConvertFile } = require('../../utils/util');
	var url = process.env.NODE_ENV == "development"? __dirname.split("node_modules")[0]+"build/NewFirmware":__dirname.split("Tools")[0]+"Services/NewFirmware";
    BufConvertFile(url, "NewFirmware").then(res=>{
    var formData = new FormData();
    formData.append('file', res);
    this.$axios_post(this.$store.state.content.device_service_ip + this.$axios_url_upload + "?req=" + this.$encrypt("sid=" + this.$store.state.content.session_id), formData).then(res=>{
        if(res.status==200){
			this.Resume();
        }else{
			ipcRenderer.sendSync('message', JSON.stringify({
				command: "notification",
				title: that.$t("notification.title_2"),
				body: that.$t("notification.body_26")
				}));
			}
      })
   })
 },

3.下载进度条

javascript 复制代码
//HTML
<div id="restart_step" style="position: relative; top: -10px; width: 0%; height: 10px; background: #226FE3; border-radius: 6px;"></div>

Resume(){
    this.Download.len == 20;
    var stop = setInterval(() => {
       if (this.Download.len >= 100) {
            clearInterval(stop);
            this.$emit("closed_ota", false);
            this.reconnect(this.action_info.ssid,this.action_info.title);
        } else {
            this.Download.len += 2;
             document.getElementById("restart_step").style.cssText += "width: " + this.Download.len + "%;";
        }
   }, 1000);
},
相关推荐
明月_清风3 小时前
🖥️ Electron 三进程 Host 实战:从架构设计到生产落地的完整指南
前端·electron·客户端
Multipath7123 小时前
5G CPE vs 5G聚合路由器 该怎么选
网络·5g·安全·智能路由器·实时音视频·信息与通信
我爱cope6 小时前
【计算机网络 | 网络层10:OSPF 与 BGP:自治系统内部和互联网之间如何路由?】
网络·学习·计算机网络·智能路由器
JienDa8 小时前
我做了一款不依赖 AI 的离线传统术数排盘工具:Electron、Vue3 与 Java 17 的完整实践
java·人工智能·electron
上海广测检测科技有限公司9 小时前
路由器CRA认证详解:欧盟网络弹性法案对联网设备的安全合规要求
网络·经验分享·安全·智能路由器
robch10 小时前
一个网络数据包是怎么从发送方路由到接受方的,整个过程中数据包里的 MAC 地址和 IP 地址是怎么变化的
网络·智能路由器
晴天1611 小时前
多个 VS Code 项目会导致 Chrome 和 Electron 应用一起卡住?-Day30
前端·chrome·electron
FW-Linker1 天前
多网聚合和卫星通信哪个更可靠?场景化选型与融合方案全解析
5g·智能路由器
刘广睿1 天前
独立开发桌面素材工作台的技术复盘:内置浏览器、下载队列与本地素材库
系统架构·electron·桌面应用·素材管理
爱丶不疚1 天前
BrowserWindow:你的Electron 应用可以不用手写红绿灯
前端·electron