Я пытаюсь обрабатывать сохраненные изображения POSTed to nodeJS (и явную фреймворку) в базу данных и испытываю определенные проблемы. Игнорируя всю веб-обработку, я думаю, что я сузил проблему до того, как кодировка base64 происходит в node. Я считаю, что приведенный ниже пример должен работать, но выходное изображение всегда повреждено.
Пример (1) загружает изображение (2) сохраняет копию if (image_orig
), чтобы подтвердить, что node может правильно прочитать файл. Это всегда работает. (3) Я беру изображение, а base64 кодирует его содержимое, (4) затем декодирует его. Конечное выходное изображение (image_decoded
) всегда повреждено.
Помощь! (node.js 0.6.0 на OSX Lion)
console.log("starting");
process.chdir(__dirname);
var fs = require("fs");
var image_origial = "image.jpg";
fs.readFile(image_origial, function(err, original_data){
fs.writeFile('image_orig.jpg', original_data, function(err) {});
var base64Image = new Buffer(original_data, 'binary').toString('base64');
var decodedImage = new Buffer(base64Image, 'base64').toString('binary');
fs.writeFile('image_decoded.jpg', decodedImage, function(err) {});
});