实现多个文件边打包边下载的功能,速度还可以,本人亲测,欢迎大家来指点
archiver --用NPM安装这个模块
---本人文件存储在file-collection 中,可以用fs :
fs.createReadStream(file)
更多带包工具请见 http://stackoverflow.com/questions/20107303/dynamically-create-and-stream-zip-to-client
var = function (response,message) {response.writehead ($); 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.col Lection. ObjectID (Ple_code)); if (!file_obj) {fail (res, ' ple notfound ');} Doc_id_arr.push ({doc_id:ple_code,doc_name:file_obj.filename});d Ocs_latest.map (function (item) {_.map (Item.docs_ AR, function (k) {var file_obj_tmp = Files.findone (new Meteor.Collection.ObjectID (k));d Oc_id_arr.push ({doc_id:k,doc_ Name:file_obj_tmp.filename});}); Res.writehead ($, {' 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);}});
Meteor---The ability to package multiple files in a zip download