简体   繁体   English

Nodejs不同的内容长度

[英]Nodejs different content-length

I'm downloading an image from internet (from Box), and I'm getting different body.length and header content-length .我正在从互联网(来自 Box)下载图像,并且得到不同的body.length和 header content-length

The response header content-length is equals to 75739 .响应头content-length等于75739 But body.length is equals to 72818 .body.length等于72818

Now look at this, if I run request(url).pipe(fs.createWriteStream('image.jpg')) , the image got 75739 and open correctly.现在看看这个,如果我运行request(url).pipe(fs.createWriteStream('image.jpg')) ,图像得到 75739 并正确打开。

Altough, if I run fs.writeFileSync('image.jpg', body) , the image got 137515 and doesn't open correctly. fs.writeFileSync('image.jpg', body) ,如果我运行fs.writeFileSync('image.jpg', body) ,图像得到 137515 并且不能正确打开。

I also did this gist so you can test by yourself: https://gist.github.com/Daymannovaes/54663e0718f0ded40285d0666f937179我也做了这个要点,所以你可以自己测试: https : //gist.github.com/Daymannovaes/54663e0718f0ded40285d0666f937179

I don't know if encoding is the problem, but I tried to get the byteLength with all the encodes and none of them was to correct size:我不知道编码是否是问题,但我尝试使用所有编码获取 byteLength,但没有一个是正确的大小:

length of the body (ascii) 72818 length of the body (utf8) 137515 length of the body (utf16le) 5636 length of the body (ucs2) 145636 length of the body (base64) 54613 length of the body (latin1) 72818 length of the body (binary) 72818 length of the body (hex) 36409

Thanks in advance!提前致谢!

I found the answer.我找到了答案。 Thanks to a friend of mine.感谢我的一个朋友。 I don't know if is fault of the request library, but if I set { encoding: 'binary' } at the request, everything works fine.我不知道是否是请求库的错误,但是如果我在请求时设置{ encoding: 'binary' } ,一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM