Javascript 使用 node.js fs.writeFile 写入二进制数据创建图像文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43487543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing binary data using node.js fs.writeFile to create an image file
提问by Koby Douek
I'm trying to write a canvas data with node.js fs.writeFileas a binary. JPEG file, but after the file is written I can see that the file is stored as plain text, not binary data.
我正在尝试使用 node.jsfs.writeFile作为二进制文件编写画布数据。JPEG 文件,但在写入文件后,我可以看到该文件存储为纯文本,而不是二进制数据。
This is an example of the datasent from the client to my node, representing the JPEG image data (just a few first characters):
这是data从客户端发送到我的节点的示例,表示 JPEG 图像数据(仅前几个字符):
/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAFA3PEY8MlBGQUZaVVBfeM...
/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAFA3PEY8MlBGQUZaVVBfeM...
I'm getting this dataon the client sideby performing:
我通过执行data在客户端得到这个:
canvas.toDataURL('image/jpeg', 0.5).replace('data:image/jpeg;base64,', '')
Here is the function usage in my node.js server:
这是我的node.js 服务器中的函数用法:
fs.writeFile('../some.jpeg', data, 'binary', function(err){});
Instead of the file being written as binary (????? JFIF ...), it writes exactly the data it received from the client.
它不是将文件写入为二进制 ( ????? JFIF ...),而是准确地写入从客户端收到的数据。
What am I doing wrong here?
我在这里做错了什么?
回答by Rayon
JavaScript language had no mechanism for reading or manipulating streams of binary data. The
Bufferclass was introduced as part of the Node.js API to make it possible to interact with octet streams in the context of things like TCP streams and file system operations.
JavaScript 语言没有读取或操作二进制数据流的机制。该
Buffer班介绍了作为Node.js的API的一部分,以使其能够与八位字节流中的事物的背景下交互象TCP流和文件系统操作。
Pure JavaScript, while great with Unicode encoded strings, does not handle straight binary data very well.
纯 JavaScript 虽然非常适合 Unicode 编码的字符串,但不能很好地处理直接的二进制数据。
When writing large amounts of data to a socket it's much more efficient to have that data in binary format vs having to convert from Unicode.
将大量数据写入套接字时,以二进制格式保存数据比从 Unicode 转换更有效。
var fs = require('fs');
// string generated by canvas.toDataURL()
var img = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0"
+ "NAAAAKElEQVQ4jWNgYGD4Twzu6FhFFGYYNXDUwGFpIAk2E4dHDRw1cDgaCAASFOffhEIO"
+ "3gAAAABJRU5ErkJggg==";
// strip off the data: url prefix to get just the base64-encoded bytes
var data = img.replace(/^data:image\/\w+;base64,/, "");
var buf = new Buffer(data, 'base64');
fs.writeFile('image.png', buf);
回答by Yakup Ad
I have had the question in question. I solved the problem when I made the default value null of "encoding" in the "request" library
我有这个问题。我在“请求”库中将“编码”的默认值设为空时解决了问题
var request = require("request").defaults({ encoding: null });
var fs = require("fs");
fs.writeFile("./image.png", body, function(err) {
if (err) throw err;
});
回答by Piush Singh
Instead of writing the file directly to your client, first, ask the server to send images in binary format.
首先,不要将文件直接写入客户端,而是要求服务器以二进制格式发送图像。
let request= {
headers: {
'Content-Type': 'image/jpeg',
'Authorization': "your token"
},
encoding:'binary'
};
request.get(url,request,(error, response, body)=>{
if(error){
console.log('error in get photo',error)
return "default image to server";
}else{
if(response.statusCode == 200){
Fs.writeFile('path',body,'binary',function(err){
if(err){
return "your message";
}else{
return "success";
}
})
}else{
console.log('error in get photo 3')
return "your message";
}
}
})

