Meteor---The ability to package multiple files in a zip download

Source: Internet
Author: User
Tags findone

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.