实现多个文件边打包边下载的功能,速度还可以,本人亲测,欢迎大家来指点archiver --用NPM安装这个模块---本人文件存储在file-collection 中,可以用fs :
fs.createReadStream(file)
更多带包工具请见 http://stackoverflow.com/questions/20107303/dynamically-create-and-stream-zip-to-client
var fail = function(response,message) { response.writeHead(200); var str = "notfound:"; if(message){ str += message; } response.end(str); }; WebApp.connectHandlers.use(‘/my/download‘,function(req, res, next) { try { var ple_code = req.query.ple_code; var cps_id = req.query.cps_id; if(ple_code === undefined || cps_id === undefined){ fail(res); } var docs_latest = Customor.find({ cps_id: cps_id }).fetch(); var doc_id_arr = []; var file_obj = Files.findOne(new Meteor.Collection.ObjectID(ple_code)); if(!file_obj){ fail(res,‘ple notfound‘); } doc_id_arr.push({ doc_id: ple_code, doc_name: file_obj.filename }); docs_latest.map(function(item) { _.map(item.docs_ar, function(k) { var file_obj_tmp = Files.findOne(new Meteor.Collection.ObjectID(k)); doc_id_arr.push({ doc_id: k, doc_name: file_obj_tmp.filename }); }); }); res.writeHead(200, { ‘Content-Type‘: ‘application/zip‘, ‘Content-disposition‘: ‘attachment; filename=myfile.zip‘ }); var archiver = Meteor.npmRequire(‘archiver‘); var zip = archiver(‘zip‘); // Send the file to the page output. zip.pipe(res); _.map(doc_id_arr,function(k){ var lolStream = Files.findOneStream(new Meteor.Collection.ObjectID(k.doc_id)); zip.append(lolStream, {name: k.doc_name}); }); zip.finalize(); } catch (error) { fail(res,error); } });
时间: 2024-10-14 12:04:01