如何使用带有base64文本的额外fs保存物理图像

时间:2015-10-16 15:41:00

标签: javascript node.js fs

拜托,你能帮帮我吗?我想使用base64保存一个用base64编码的图像。

我试过这个:

var image = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAgAAAQABAAD/7QCcUGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAAIAcAmcAFFd6cEFHZWltVEJyLURXbzBWeFlNHAIoAGJGQk1EMDEwMDBhYzAwMzAwMDBlOTA5MDAwMDRjMTIwMDAwOWMxMzAwMDAwNTE1MDAwMDlhMTkwMDAwOGMyMzAwMDBjNDI0MDAwMDRkMjYwMDAwZDkyNzAwMDBjYTM4MDAwMP/iAhxJQ0NfUFJPRklMRQABAQAAAgxsY21zAhAAAG1udHJSR0IgWFlaIAfcAAEAGQADACkAOWFjc3BBUFBMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD21gABAAAAANMtbGNtcwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACmRlc2MAAAD8AAAAXmNwcnQAAAFcAAAAC3d0cHQAAAFoAAAAFGJrcHQAAAF8AAAAFHJYWVoAAAGQAAAAFGdYWVoAAAGkAAAAFGJYWVoAAAG4AAAAFHJUUkMAAAHMAAAAQGdUUkMAAAHMAAAAQGJUUkMAAAHMAAAAQGRlc2MAAAAAAAAAA2MyAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHRleHQAAAAARkIAAFhZWiAAAAAAAAD21gABAAAAANMtWFlaIAAAAAAAAAMWAAADMwAAAqRYWVogAAAAAAAAb6IAADj1AAADkFhZWiAAAAAAAABimQAAt4UAABjaWFlaIAAAAAAAACSgAAAPhAAAts9jdXJ2AAAAAAAAABoAAADLAckDYwWSCGsL9hA/FVEbNCHxKZAyGDuSRgVRd13ta3B6BYmxmnysab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hiNRYrT4Iv8ngs9lCn+hkyTp+ZwMgvRJMGG+4kT9sbs+haQFbsm2JRCEIaTREbRC4gVDWmMoHYguUKj7/YtrlbyQ4/QFBK6PhvoZBeh4luERrOJos2Y4Tx3oZFxBB05ECIkhQUDCEuR+lhnwOYzJfhkXLGiYDvwxi+UWw/fBZSQb7tOGQWubLdIyxLfzRIyFl4N4WVbKx5nejmxugN5N4QNNm6yRwEJEkmSJJGtbKN9o4WJ5GxWqtrMgj9zgZNUQWf1MbDlsiVM8BlJsaDe9EYdm5V8ITgqRrMSGdl7GNfSkglkQv+7RKNJJVwQNyP3OBk1g5sJImdbwgWxjrbE+bHc7I8X92INnvyFHVSSOaS4RDovLVpfZnE/shhVqFioQzqyebDAGTWMs0e5YngY084Fv8ALIn7MFcFDeb0qCQmQ5";
    image = image.replace(/^data:image\/png;base64,/, "").replace(/^data:image\/jpeg;base64,/, "").replace(/^data:image\/gif;base64,/, "");
    
    var image2 = "9j/4AAQSkZJRgABAgAAAQABAAD/7QCcUGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAAIAcAmcAFFd6cEFHZWltVEJyLURXbzBWeFlNHAIoAGJGQk1EMDEwMDBhYzAwMzAwMDBlOTA5MDAwMDRjMTIwMDAwOWMxMzAwMDAwNTE1MDAwMDlhMTkwMDAwOGMyMzAwMDBjNDI0MDAwMDRkMjYwMDAwZDkyNzAwMDBjYTM4MDAwMP/iAhxJQ0NfUFJPRklMRQABAQAAAgxsY21zAhAAAG1udHJSR0IgWFlaIAfcAAEAGQADACkAOWFjc3BBUFBMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD21gABAAAAANMtbGNtcwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACmRlc2MAAAD8AAAAXmNwcnQAAAFcAAAAC3d0cHQAAAFoAAAAFGJrcHQAAAF8AAAAFHJYWVoAAAGQAAAAFGdYWVoAAAGkAAAAFGJYWVoAAAG4AA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InV6fucDJ6ng8CF2csmnkmsKJtJFGMhgxwZ9CqbJyPA+UMSA0M2lE6341en5nAyevB+aGtlGcM7ZLuUciKgg0q8m9IciQQHeJEcjdieU8PJhD0GtZ50Y9PzOBk9WDI72sZvzo+BEenXdZPDkNgepq9gaSktiwhEnXmDcKxhNrPT8zgZPVLMDGnMIQMTRcg8JVBvZFvZ+HZYYd8jO7rcgYqVjLyPojHlGK/mLGjHowyep0kwOTHAvJkGyreRNAUoid+yKdU4oiaGYRMUU7bFNb6QZZ5eRsRqBqk8nkeU0Y9ODy9OYaP3mvJc7T6LtmQZkQxp27+i6WeTDkG3I0UxPIcMMpSNMGT3eh+GR2jGfmcDJ6UlHcvAmfcDoZ83RQjQ9Qh7hxUmOSy0hiNRYrT4Iv8ngs9lCn+hkyTp+ZwMgvRJMGG+4kT9sbs+haQFbsm2JRCEIaTREbRC4gVDWmMoHYguUKj7/YtrlbyQ4/QFBK6PhvoZBeh4luERrOJos2Y4Tx3oZFxBB05ECIkhQUDCEuR+lhnwOYzJfhkXLGiYDvwxi+UWw/fBZSQb7tOGQWubLdIyxLfzRIyFl4N4WVbKx5nejmxugN5N4QNNm6yRwEJEkmSJJGtbKN9o4WJ5GxWqtrMgj9zgZNUQWf1MbDlsiVM8BlJsaDe9EYdm5V8ITgqRrMSGdl7GNfSkglkQv+7RKNJJVwQNyP3OBk1g5sJImdbwgWxjrbE+bHc7I8X92INnvyFHVSSOaS4RDovLVpfZnE/shhVqFioQzqyebDAGTWMs0e5YngY084Fv8ALIn7MFcFDeb0qCQmQ5";
    
    fse.writeFile("image.png", image, 'base64', function(err) {
        if(!err)
            console.log('cool ok');
        else
            console.log('erreur' + err);
    });

控制台日志向我显示图像已成功创建,但无法打开它。当我尝试查看图像时,它显示错误(文件已损坏)。

你能帮帮我吗?

请原谅我的错误,我不太擅长英语。

2 个答案:

答案 0 :(得分:2)

使用Buffer将ascii转为二进制,然后将binary添加到fs.writeFile

var fs = require('fs');
var binImage = new Buffer(image, 'base64').toString('binary');
fs.writeFile("image.jpg", binImage, "binary", function(err) {
    if(!err)
        console.log('cool ok');
    else
        console.log('erreur' + err);
});

答案 1 :(得分:0)

将正则表达式更改为:

 image = image.replace(/^data:image\/(png|png|gif|bmp|jpg|jpeg);base64,/, "");

我在node.js上对它进行了测试,并且它可以处理和渲染您编码的小鸡图像。