向客户端发送缓冲区以进行下载

Sending a buffer to the client to download

我的 Node.js 服务器上有一个来自 Dropbox 的 Buffer 下载。我想将该缓冲区(或将其转换为文件并发送)发送到客户端,并让它立即开始在客户端下载。我在这里错过了什么?

var client = DBoxApp.client(req.session.dbox_access_token);

client.get(req.body.id, function(status, data, metadata) {
    // WHAT DO I DO HERE?
})

这是我的 angular(使用承诺)。当我 console.log(响应我得到一个包含缓冲区的对象)时。

function(id, cloud){
  return $http.post('/download/'+cloud, {id: id}).then(function(response){
    console.log(response)
  }, function(response) {
    return $q.reject(response.data);
  })
}

您使用的 dbox 模块似乎有一个 stream() 选项,它可能更适合文件下载。您还应该调用 metadata 来查找文件的 MIME 类型。例如:

var middleware = function(req, res, next) {
    var client = DBoxApp.client(req.session.dbox_access_token);
    var file = req.body.id;

    client.metadata(file, function(status, reply) {
        res.setHeader('Content-disposition', 'attachment; filename=' + file);
        res.setHeader('Content-type', reply.mime_type);
        client
            .stream(file)
            .pipe(res)
            .on('error', next);
    });
};