从 node.js 服务器下载文件

Posted

技术标签:

【中文标题】从 node.js 服务器下载文件【英文标题】:Download file from node.js server 【发布时间】: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()));

现在如何发送给客户?

【问题讨论】:

【参考方案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

【讨论】:

以上是关于从 node.js 服务器下载文件的主要内容,如果未能解决你的问题,请参考以下文章