通过Node.js的http.ServerResponse发送二进制缓冲区客户端 [英] Send a binary Buffer to client through http.ServerResponse in Node.js

查看:118
本文介绍了通过Node.js的http.ServerResponse发送二进制缓冲区客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在缓冲区对象的二进制数据(如图像文件)(而非文件),并希望通过http.ServerResponse服务于原始的二进制数据的客户端。
我该怎么办呢?

I have a binary data(like image file) in Buffer object(not file), and want to serve the raw binary data to client through http.ServerResponse. How can I do it ?

推荐答案

我设法找出答案。只需添加二进制编码都写()和end()。

I managed to find out the answer. Just add "binary" encoding to both write() and end().

    res.write(buffer,'binary');
    res.end(null, 'binary');

请注意,这两个写和结束功能需要指定的二进制编码。否则,缓冲区EN codeD为UTF-8。
(所以,JPEG头FF FF D8 E0将是C3 BF C3 98 C3 BF C3 A0......)

Note that both "write" and "end" function requires the 'binary' encoding specified. Otherwise, the buffer is encoded as UTF-8. (So, JPEG header "ff d8 ff e0" will be "c3 bf c3 98 c3 bf c3 a0"...)

这篇关于通过Node.js的http.ServerResponse发送二进制缓冲区客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆