zoukankan      html  css  js  c++  java
  • 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);
    }
    
    });
    

      

  • 相关阅读:
    第二十九课 循环链表的实现
    第二十八课 再论智能指针(下)
    第二十七课 再论智能指针(上)
    第二十六课 典型问题分析(Bugfix)
    普通new和placement new的重载
    leetcode 581. Shortest Unsorted Continuous Subarray
    leetcode 605. Can Place Flowers
    leetcode 219. Contains Duplicate II
    leetcode 283. Move Zeroes
    leetcode 217. Contains Duplicate
  • 原文地址:https://www.cnblogs.com/shenggen/p/4572268.html
Copyright © 2011-2022 走看看