node.js - 发送缓冲区到客户端进行下载

标签 node.js express dropbox-api

我在我的 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?
})

这是我的角度(使用 promise )。当我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() 选项,该选项可能更适合文件下载。您还应该调用元数据来查找文件的 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);
    });
};

关于node.js - 发送缓冲区到客户端进行下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30225404/

相关文章:

node.js - Firebase Cloud Functions 中抛出未处理的错误

web-services - 为什么 Railway IS 不被视为 RESTful Web 服务器?

angularjs - 类型错误 : Dbschema(Mongoose Schema) is not a function

javascript - Webpack:使用 Bundle.js, "React is not defined"

node.js - Express Handlebars 不会渲染数据

node.js - 类中的静态变量在 app.listen 的回调中未定义

Dropbox访问 token 到期

android - Dropbox Sync API - 不满意的链接错误

java - 如何在 DROPBOX 中使用 MULTIPART 文件上传来上传文件

node.js - 带有逻辑 AND 的 swagger Node 多个安全处理程序