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);
},
相关推荐
鲁邦通物联网12 小时前
海外充电桩工业路由器怎么选?从通信准入到双SIM冗余的工程设计指南
智能路由器·工业路由器·工业级路由器·5g路由器·5g工业路由器
vipjx112 小时前
百度网盘下载慢解决思路:2026最新PanDownload与直链工具对比
网络·智能路由器
星创易联13 小时前
Robotaxi远程平行驾驶通信保障:5G车载路由器低时延与网络切片实践
5g·车载系统·智能路由器·车载通信·车载网关
河北之花1 天前
计算机三级:BGQ
网络·tcp/ip·计算机网络·智能路由器
星恒讯工业路由器1 天前
四张网协同演进:新一代通信网对工业通信设备意味着什么?
网络·物联网·智能路由器·工业路由器·工业物联网·5g-a·新一代通信网
忆凡_2 天前
Dyad 鸿蒙 PC 适配全记录:让本地 AI 应用构建器进入 HarmonyOS PC
electron·鸿蒙
疯狂打码的少年2 天前
【计算机网络】网络互连设备(中继器 / 网桥 / 交换机 / 路由器 / 网关)
网络·笔记·计算机网络·智能路由器
zbtlink2 天前
家用全屋WiFi技术选型思考:吸顶AP、面板AP与路由器的参数对比
网络·智能路由器
心态还需努力呀2 天前
把 Zettlr 搬上鸿蒙 PC:一次 Electron 应用移植实战
华为·electron·harmonyos
Multipath7123 天前
远程可视操控:多链路聚合如何把“不确定”变“可管理”
网络·网络协议·5g·智能路由器·信息与通信