electron自己有下载更新安装包的方法,但是公司要求配置文件和下载地址不一致,身为一个小菜鸟不知道怎么改都成功不了于是换一种方法自己下载安装包然后启动它。
//获取并运行exe文件
var cp = require("child_process");
downloadExe(file_url, targetPath) {
let that = this;
var received_bytes = 0;
var total_bytes = 0;
var exePath = path.dirname(remote.app.getAppPath());
var req = request({method: "GET",url: file_url});
let arr = file_url.split("/");
var out = fs.createWriteStream(exePath + arr[arr.length - 1]);
req.pipe(out);
req.on("response", function(data) {
total_bytes = parseInt(data.headers["content-length"]);
});
req.on("data", function(chunk) {
received_bytes += chunk.length;
that.percent = parseFloat(
((received_bytes / total_bytes) * 100).toFixed(0)
);
});
req.on("end", function() {
//下载成功,解压覆盖
cp.exec(exePath + "/text.exe",[],(error, stdout, stderr) =>{
console.log(error, stdout, stderr)
});
});
},
方法和我上一篇热更新的方法很像,不同的是一个是解压覆盖一个是运行,重点就一个exec的方法。