我在我的 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/