Node.js 使用二进制文件将 http.ClientRequest 通过管道传输到 fs.createWriteStream 会导致文件损坏/膨胀
我正在处理的 Node.js (v0.4.12) 模块遇到了一个奇怪的问题。它是一个 CouchDB 模块,虽然我很确定 CouchDB 不是这里的问题,但我将包含它,因为它可能是有用的信息。
无论如何,我正在编写一个用于使用 CouchDB 上传和下载附件的包装器。我的上传部分工作得很好,但下载部分我遇到了困难。
我的测试过程是这样的:
- 上传图像文件(
http.请求
,fs.createReadStream
) - 下载文件 (
http.request
) - 将下载的文件保存到不同位置以进行比较/损坏检查 (
fs.createWriteStream
,stream.pipe
)
这有效明文即可文件,但像图像这样的二进制文件的行为有所不同。
正如我之前提到的,上传看起来不错。因为我可以访问 CouchDB 中的 URL 并查看图像。 (大小匹配并且呈现我上传的图像)当我使用我的模块下载文件并保存它时,输出文件比源文件大(大50-100%),并且无法由任何图像打开编辑们。 (因此,它以某种方式“损坏”)
当涉及到二进制文件时,我是否遗漏了有关 Streams 的某些内容?我尝试将两侧的 encoding
更改为 "binary"
和 "base64"
,但输出文件仍然比来源。在我发现编码选项之前,它被设置为 "utf8"
,并且输出文件大了 100%,所以这让我认为这是某个地方的编码问题。
我还尝试过手动管道传输流(而不是使用 pipe
方法本身),但结果是相同的。
I'm having a weird issue with a Node.js (v0.4.12) module I am working on. It's a CouchDB module, and while I'm pretty sure CouchDB is not the problem here, I'll include it as it may be useful information.
Anyways, I'm writing a wrapper for uploading and downloading attachments with CouchDB. I have the upload part working very well, it's the download part I am having difficulty with.
My test process is this:
- Upload image file (
http.request
,fs.createReadStream
) - Download file (
http.request
) - Save downloaded file to different location for comparison/corruption checking (
fs.createWriteStream
,stream.pipe
)
This works fine with plaintext files, but binary files like images behaves differently.
As I mentioned before, the upload appears fine. As I can visit the URL in CouchDB and view the image. (the size matches and it renders the image I uploaded) When I use my module to download the file and save it, the output file is larger than the source file, (50-100% larger) and it cannot be opened by any image editors. (hence, it is "corrupted" somehow)
Am I missing something about Streams when it comes to binary files? I've tried changing the encoding
on both sides to "binary"
and "base64"
, but the output file is still 50% larger than the source. Before I discovered the encoding option, it was set to "utf8"
, and the output file was 100% larger, so that leads me to think this is an encoding issue somewhere in there.
I've also tried manually piping the streams (rather than using the pipe
method itself) but the result is the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
抱歉,如果我问错了这个问题,但看来你是
尝试对二进制文件使用编码。你不需要
to: 节点默认为您提供带有二进制数据的原始缓冲区。
这是我下载小丑的一个例子:
我希望这有帮助。
Sorry if I got this question wrong but it seems you are
trying to use encodings with a binary file. You don't need
to: node gives you raw buffers with binary data by default.
Here is an example of me downloading a clown:
I hope this helps.
查看:
https://github.com/joyent/node/issues/1569
Node 没有就像http中的换行符一样。更新您的节点。
如果您正在寻找 couchdb,我的博客上有一篇关于该主题的博客文章,网址为writings.nunojob.com。作为沙发客户端,使用 nano 通常是一个不错的选择
Check:
https://github.com/joyent/node/issues/1569
Node doesn't like newlines in http. Update your node.
If you are looking for couchdb I have a blog post on the subject on my blog at writings.nunojob.com. Using nano is usually a good choice as a couch client