Javascript 如何从Node.JS套接字流触发浏览器下载?
my node.js应用程序通过Javascript 如何从Node.JS套接字流触发浏览器下载?,javascript,node.js,express,Javascript,Node.js,Express,my node.js应用程序通过var socket=net.createConnection(端口,ip)连接从另一台服务器下载文件。一旦建立连接,服务器就会将文件作为数据发送 然后我通过做动作来抓住它 socket.on('data', function(data) { }).on('connect', function() { }).on('end', function() { console.log('DONE'); }); 我最初的目标是,使用上述方法下载文件,同时将字节作为可下
var socket=net.createConnection(端口,ip)连接
从另一台服务器下载文件。一旦建立连接,服务器就会将文件作为数据发送
然后我通过做动作来抓住它
socket.on('data', function(data) {
}).on('connect', function() {
}).on('end', function() {
console.log('DONE');
});
我最初的目标是,使用上述方法下载文件,同时将字节作为可下载文件提供给客户端浏览器。例如:用户单击站点上的一个按钮,该按钮将触发服务器端下载功能,用户将得到文件保存提示。然后Node.JS从远程服务器下载文件,同时在浏览器客户端将每个新字节提供给用户。这可能吗?我想它需要发送八位字节流的头来触发browser Node.JS之间的文件传输。但是怎么做呢
更新
现在,我在下面的答案的帮助下尝试了下面的代码:
app.get('/download', function (req, res) {
res.setHeader('Content-disposition', 'attachment; filename=' + "afile.txt");
res.setHeader('Content-Length', "12468")
var socket = net.createConnection(1024, "localhost");
console.log('Socket created.');
socket.on('data', function(data) {
socket.pipe(res)
}).on('connect', function() {
// // Manually write an HTTP request.
// socket.write("GET / HTTP/1.0\r\n\r\n");
}).on('end', function() {
console.log('DONE');
socket.end();
});
});
数据将作为下载发送到用户的浏览器,但最终结果是一个损坏的文件。我检查了其中的内容,发现过程中有什么东西导致文件损坏。
我想现在我必须一个字节一个字节地写?您需要在http响应中设置头,而不是执行socket.pipe?:
response.writeHead(200, {
'Content-Disposition': 'attachment; filename=genome.jpeg; modification-date="Wed, 12 Feb 1997 16:29:51 -0500"'
});
yourDataStream.pipe(response);
请参见您需要在http响应中设置标头:
response.writeHead(200, {
'Content-Disposition': 'attachment; filename=genome.jpeg; modification-date="Wed, 12 Feb 1997 16:29:51 -0500"'
});
yourDataStream.pipe(response);
请参见看起来您可能需要:
app.get('/download', function (req, res) {
res.attachment('afile.txt');
require('http').get('http://localhost:1234/', function(response) {
response.pipe(res);
}).on('error', function(err) {
res.send(500, err.message);
});
});
看起来您可能需要以下内容:
app.get('/download', function (req, res) {
res.attachment('afile.txt');
require('http').get('http://localhost:1234/', function(response) {
response.pipe(res);
}).on('error', function(err) {
res.send(500, err.message);
});
});
我找到了解决办法!
通过执行res.write(d),我能够将其他连接的字节定向到用户浏览器下载
app.get('/download', function (req, res) {
res.setHeader('Content-disposition', 'attachment; filename=' + "afile.jpg");
res.setHeader('Content-Length', "383790");
res.setHeader('Content-Type','image/jpeg');
var socket = net.createConnection(1024, "localhost");
console.log('Socket created.');
//socket.setEncoding("utf8");
socket.on('data', function(d) {
console.log(d);
res.write(d);
}).on('connect', function() {
// // Manually write an HTTP request.
// socket.write("GET / HTTP/1.0\r\n\r\n");
}).on('end', function() {
console.log('DONE');
socket.end();
});
});
我找到了解决办法!
通过执行res.write(d),我能够将其他连接的字节定向到用户浏览器下载
app.get('/download', function (req, res) {
res.setHeader('Content-disposition', 'attachment; filename=' + "afile.jpg");
res.setHeader('Content-Length', "383790");
res.setHeader('Content-Type','image/jpeg');
var socket = net.createConnection(1024, "localhost");
console.log('Socket created.');
//socket.setEncoding("utf8");
socket.on('data', function(d) {
console.log(d);
res.write(d);
}).on('connect', function() {
// // Manually write an HTTP request.
// socket.write("GET / HTTP/1.0\r\n\r\n");
}).on('end', function() {
console.log('DONE');
socket.end();
});
});
您不能这样做,但是您可以使用套接字在内存中接收整个文件,然后让用户使用数据URI下载该文件。像这样:。但是,使用websockets无法直接为用户下载流式传输文件。文件下载只能通过http进行。websocket没有这个接口,因为通过它发送文件还没有标准化。请看另一个类似的问题。您正在混合('data')和管道。附加
.pipe
时,第一块数据会丢失。您不能这样做,但可以使用套接字在内存中接收整个文件,然后让用户使用数据URI下载该文件。像这样:。但是,使用websockets无法直接为用户下载流式传输文件。文件下载只能通过http进行。websocket没有这个接口,因为通过它发送文件还没有标准化。请看另一个类似的问题。您正在混合('data')和管道。附加.pipe
文件通过websocket发送时,第一个数据块丢失。为什么不能通过http提供服务?可以,但这不是OP要求的。我看不到问题中对websocket的引用。所有问题都可以通过普通的http/Ajax实现对不起,也许你是对的。我假设了捕获浏览器上运行的文件的代码。否则,您的回答是正确的。文件是通过websocket发送的。为什么您不能通过http提供服务?您可以,但这不是OP要求的。我看不到问题中对websocket的引用。所有问题都可以通过普通的http/Ajax实现对不起,也许你是对的。我假设捕获文件的相关代码在浏览器上运行。否则,你的回答是正确的。