Node.js Base64 Image decoding and writing to file

Mehdi picture Mehdi · Apr 6, 2012 · Viewed 26.9k times · Source

I'm sending the contents of this Flex form (Don't ask why) over to node. There is a post paramteter called "photo" which is a base64 encoded image.

Contents of photo get sent over ok. Problem is when I am trying to decode the content and write them to a file.

  var fs = require("fs");

  fs.writeFile("arghhhh.jpg", new Buffer(request.body.photo, "base64").toString(), function(err) {});

I've tried toString("binary") as well. But it seems node doesnt decode all of the content. It seems it only decodes jpg header info and leaves the rest.

Can anyone please help me with this?

Thanks

Answer

Nathan Friedly picture Nathan Friedly · Apr 6, 2012

Try removing the .toString() entirely and just write the buffer directly.