Node.js Base64 Image decoding and writing to file

3,234 views
Skip to first unread message

Mehdi Avdi

unread,
Apr 5, 2012, 7:57:30 PM4/5/12
to nod...@googlegroups.com

Hi everyone,

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

mscdex

unread,
Apr 5, 2012, 8:32:47 PM4/5/12
to nodejs
On Apr 5, 7:57 pm, Mehdi Avdi <mehdi.a...@gmail.com> wrote:
> 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.

Did you try not using toString() at all?:

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

dvbportal

unread,
Apr 6, 2012, 2:56:44 AM4/6/12
to nod...@googlegroups.com
It could be due to chunking that the body is not yet complete when your code runs.

Jann Horn

unread,
Apr 11, 2012, 3:58:39 PM4/11/12
to nod...@googlegroups.com
On Thu, Apr 05, 2012 at 04:57:30PM -0700, Mehdi Avdi wrote:
> 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) {});

That's totally wrong. Leave out the .toString():

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

hughlomas

unread,
Apr 12, 2012, 5:28:19 PM4/12/12
to nod...@googlegroups.com
Check out  "inconsistency of base64 encoding on http.get responses"  @ https://github.com/joyent/node/issues/3026
Reply all
Reply to author
Forward
0 new messages