文件下载完成后运行文件

时间:2013-10-10 01:38:19

标签: javascript node.js stream download node-webkit

我是node-webkit的新手。我使用以下代码下载文件。文件完成后如何自动运行文件?

var https = require('https');
var fs = require('fs');

var file = fs.createWriteStream("update_setup.exe");
var request = https.get(url + "/appdata/update_setup.exe", function (response) {
  response.pipe(file);
});

1 个答案:

答案 0 :(得分:3)

只需使用可写流的close事件并生成子进程。响应完成对流的管道后,事件将触发。

var https = require('https');
var fs = require('fs');
var exec = require('child_process').exec;

var file = fs.createWriteStream('update_setup.exe');
var request = https.get(path, function(res) {
  res.pipe(file);
});

file.on('close', function() {
  exec('update_setup.exe', function(err, stdout, stderr) {
    // output from starting
  });
});