NodeJS 编写 base64 图像文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20267939/
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
NodeJS write base64 image-file
提问by marcel
My NodeJS-Server receives a picture base64 encoded.
我的 NodeJS-Server 收到一张 base64 编码的图片。
data:image/jpeg;base64,/9j/4QCcRXhpZgAASUkqAAgAAAA ... CiiigD//Z
The received data should be saved as jpg. Therefore I use a Buffer and the FileSystemWriter:
接收到的数据应保存为jpg。因此我使用缓冲区和 FileSystemWriter:
var imageBuffer = new Buffer(data, 'base64'); //console = <Buffer 75 ab 5a 8a ...
fs.writeFile("test.jpg", imageBuffer, function(err) { //... });
the fs.writeFile doesn't call an error. A jpeg-file is saved, but I can't open it. Image-Viewer says:
fs.writeFile 不会调用错误。保存了一个 jpeg 文件,但我无法打开它。图像查看器 说:
File is damaged or too big.
The original file is 6kb large and the new file 7kb.
原始文件大 6kb,新文件大 7kb。
回答by Julian Lannigan
You have to strip the url meta information from it, the data:image/jpegpart. (Reiterating what @CBroe said) Here is a small function to return the correct information from the input string.
您必须从中删除 url 元信息,即data:image/jpeg部分。(重申@CBroe 所说的)这是一个从输入字符串返回正确信息的小函数。
var data = 'data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAA..kJggg==';
function decodeBase64Image(dataString) {
var matches = dataString.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/),
response = {};
if (matches.length !== 3) {
return new Error('Invalid input string');
}
response.type = matches[1];
response.data = new Buffer(matches[2], 'base64');
return response;
}
var imageBuffer = decodeBase64Image(data);
console.log(imageBuffer);
// { type: 'image/jpeg',
// data: <Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 00 b4 00 00 00 2b 08 06 00 00 00 d1 fd a2 a4 00 00 00 04 67 41 4d 41 00 00 af c8 37 05 8a e9 00 00 ...> }
Then you can save the buffer using your above method.
然后您可以使用上述方法保存缓冲区。
fs.writeFile('test.jpg', imageBuffer.data, function(err) { ... });
回答by rolnn
Another way is to use fs.writeFilewith encoding option base64after stripping out the meta information.
另一种方法是在剥离元信息后使用fs.writeFilewith encoding 选项base64。
var image = 'data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAA..kJggg==';
var data = image.replace(/^data:image\/\w+;base64,/, '');
fs.writeFile(fileName, data, {encoding: 'base64'}, function(err){
//Finished
});
回答by kumbhanibhavesh
try this simple way
试试这个简单的方法
var imgData = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA..kJggg==';
var base64Data = imgData.replace(/^data:image\/png;base64,/, "");
require("fs").writeFile("out.png", base64Data, 'base64',
function(err, data) {
if (err) {
console.log('err', err);
}
console.log('success');
});
focuse at here 1) data:image/png;basethere is png2) replace(/^data:image\/png;here too pngand name must save with 3) writeFile("out.pngpng
focuse at here 1) data:image/png;basethere is png2) replace(/^data:image\/png;here too pngand name must save with 3)writeFile("out.pngpng
回答by K.S
Here you can use this way little changes I did
在这里你可以用这种方式我做的小改动
var imgData = req.body.image;// coming from client request
var base64Data = imgData.split(",")[1];// split with `,`
require("fs").writeFile(Date.now()+"filename.jpeg", base64Data, 'base64',
function(err, data) {
if (err) {
console.log('err', err);
}
console.log(data,"data");
and finally your file will be looks like 1572341624757filename.jpeg
最后你的文件看起来像 1572341624757filename.jpeg

