【发布时间】:2015-03-15 12:03:17
【问题描述】:
我有一个很大的 JSON 块;将其作为(下载的)文件发送到浏览器的最佳方式是什么?
这是一个示例,如果我的源来自文件,但在我的情况下,我的源不是文件,它是一个变量,我需要将此 json 数据发送到浏览器:
var http = require('http');
var fs = require('fs');
var file = fs.createWriteStream("file.jpg");
var request = http.get("http://i3.ytimg.com/vi/J---aiyznGQ/mqdefault.jpg", function(response) {
response.pipe(file);
});
我尝试的代码:
res.setHeader('Content-disposition', 'attachment; filename=myData.json');
res.setHeader('Content-type', 'application/json');
//data is my variable with my JSON block
res.pipe(data);//This is not right!
如果我的问题含糊不清,如果您需要更多说明,请告诉我
【问题讨论】:
-
这段代码的实际结果是什么?
-
@DanielKaplan 对于我尝试的代码:错误:Object #
-
试试
res.write(data, "binary")或res.write(new Buffer(data.toString("binary"), "binary")); -
@bhantol 他们都没有工作;第一个说第一个参数需要缓冲,第二个的加载永远不会结束!顺便说一句,我将它们与我尝试的标题一起使用。
-
试试
just res.end(new Buffer(data, "binary"))
标签: javascript json node.js file express