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);
},
相关推荐
upgrador6 小时前
桌面应用开发:Electron 与 NSIS 的关系、打包流程及 Windows 本地构建实战
javascript·windows·electron
zbtlink1 天前
给自动售货机配的那台WiFi6路由器,它用上了吗
网络·智能路由器
爱刷碗的苏泓舒1 天前
网络通信入门:交换机、路由器与防火墙的角色、关系、功能和使用场景
网络·智能路由器·网络通信·防火墙·交换机
盒子里的加菲猫1 天前
什么?不会C语言也可以搭建窗口级应用程序?(Electron)
c语言·开发语言·electron
hongmai6668882 天前
ESP32-C5-WROOM-1-N16R8:双频Wi-Fi 6与多协议融合,重新定义物联网连接新标准
笔记·嵌入式硬件·物联网·智能路由器·risc-v
Shell运维手记3 天前
ARP 协议超详细讲解(适合网工考试 / 运维理解)
运维·网络·网络协议·智能路由器
薛定谔的猫-菜鸟程序员3 天前
基于 Electron 的本地短视频解析与下载工具:架构设计与工程实践
java·electron·音视频
白狐_7983 天前
【408计算机网络|第04章·下|408-CN-04B】网络层(下):路由算法、RIP、OSPF、BGP、IPv6与路由器
计算机网络·算法·智能路由器
Inhand陈工3 天前
路由器专题二:有线为主,蜂窝备份——映翰通路由器链路备份功能详解
运维·网络·物联网·智能路由器·系统安全
imber3 天前
别把多平台发布当复制粘贴:一套可复用的内容工程工作流
electron