router.get(‘/download/qiantu‘,function(req,res){ var dirname = (new Date()).getTime() var zippath = ‘./down/‘+dirname+‘.zip‘ var output = fs.createWriteStream(zippath) var ziparchiver = archiver(‘zip‘,{ store:true }) ziparchiver.pipe(output) async.mapSeries(req.session.qtimagesurl,function(item, callback){//异步下载 setTimeout(function(){ //downloadPic(item, dir+‘/‘+ (new Date()).getTime() +‘.jpg‘); ziparchiver.append(request(item),{‘name‘:‘images/‘+(new Date()).getTime() +‘.jpg‘}) callback(null, item); },400); }, function(err, results){ req.session.qtimagesurl =[] //下载完成后清空 ziparchiver.finalize() res.render(‘index‘) }); })
request(item),请求远程图片文件用request,本地文件就用fs.createReadStream
item为图片路径
var output = fs.createWriteStream(zippath)这个是压缩文件名字
{‘name‘:‘images/‘+(new Date()).getTime() +‘.jpg‘}
压缩文件解压后的命名,/前面会自动生产相应文件夹
ziparchiver.finalize()结束操作
时间: 2024-12-22 00:43:43