【问题标题】:Download file from node.js server从 node.js 服务器下载文件
【发布时间】:2018-06-22 10:59:30
【问题描述】:

在我的 metor/react 应用程序中,我有创建受保护 zip 文件的服务器方法。

var image = fs.readFileSync(filePath);
var text = new Buffer(data);
var user_data = Meteor.user()._id;

mz.append(`${user_data}/data.json`, text, {
    password: password
});

mz.append(`${user_data}/national-id${path.extname(fileName)}`, image, {
    password: password
});

fs.writeFileSync(`${dir}/Cryptopass.zip`, new Buffer(mz.zip()));

现在如何发送给客户?

【问题讨论】:

    标签: javascript node.js file meteor save


    【解决方案1】:

    使用 Express 应该很简单:

    const express = require('express');
    var app = express();
    
    app.route("/downloadzip").get(function(req,res){
    
        var mz = new Minizip();
        var image = fs.readFileSync(filePath);
    
        var text = new Buffer(data);
        var user_data = Meteor.user()._id;
    
        mz.append(`${user_data}/data.json`, text, {password: password});
        mz.append(`${user_data}/national-id${path.extname(fileName)}`, image, {password: password});
    
        res.type('zip').send(new Buffer(mz.zip()));
    })
    
    app.listen(8081);
    

    使用 curl 测试这条路线:

    curl http://localhost:8081/downloadImage > test.zip
    

    【讨论】:

      猜你喜欢
      • 2021-01-30
      • 2021-09-28
      • 1970-01-01
      • 2013-08-30
      • 2017-07-06
      • 2019-03-26
      相关资源
      最近更新 更多