Javascript 下载文件后执行某些操作
一般来说,我对编程有点陌生。我的问题是我想下载一个文件,然后做一些事情Javascript 下载文件后执行某些操作,javascript,node.js,Javascript,Node.js,一般来说,我对编程有点陌生。我的问题是我想下载一个文件,然后做一些事情 Danbooru.search(tag, function (err, data) { //search for a random image with the given tag data.random() //selects a random image with the given tag .getLarge() //get's a link to the image .pip
Danbooru.search(tag, function (err, data) { //search for a random image with the given tag
data.random() //selects a random image with the given tag
.getLarge() //get's a link to the image
.pipe(require('fs').createWriteStream('random.jpg')); //downloads the image
});
现在,我想在下载文件后执行console.log。我不想使用setTimeout,因为下载文件需要不同的时间
谢谢你的帮助。看看这是否适合你。只需将请求保存到一个变量,并检查其上的
finish
事件
Danbooru.search(tag, function (err, data) {
var stream = data.random()
.getLarge()
.pipe(require('fs').createWriteStream('random.jpg'));
stream.on('finish', function() { console.log('file downloaded'); });
});