meteor---在合并打包多个文件ZIP下载的功能

实现多个文件边打包边下载的功能,速度还可以,本人亲测,欢迎大家来指点
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);
}

});

  

转载于:https://www.cnblogs.com/shenggen/p/4572268.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值