Node.js Base64 Image decoding and writing to file

综合技术 2018-02-13

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(, "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?


Problem courtesy of: Mehdi


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

Solution courtesy of: Nathan Friedly


Remove .toString()

Here you decode the base64 to a buffer, which is fine, but then you convert the buffer into a string. This means that it is a string object whose code points are bytes of the buffer.

Discussion courtesy of: sam100rav

this is my full solution which would read any base64 image format, decode it and save it in the proper format in the database:

// Save base64 image to disk
        // Decoding base-64 image
        // Source:
        function decodeBase64Image(dataString) 
          var matches = dataString.match(/^data:([A-Za-z-+/]+);base64,(.+)$/);
          var response = {};

          if (matches.length !== 3) 
            return new Error('Invalid input string');

          response.type = matches[1];
 = new Buffer(matches[2], 'base64');

          return response;

        // Regular expression for image type:
        // This regular image extracts the "jpeg" from "image/jpeg"
        var imageTypeRegularExpression      = //(.*?)$/;      

        // Generate random string
        var crypto                          = require('crypto');
        var seed                            = crypto.randomBytes(20);
        var uniqueSHA1String                = crypto

        var base64Data = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAZABkAAD/4Q3zaHR0cDovL25zLmFkb2JlLmN...';

        var imageBuffer                      = decodeBase64Image(base64Data);
        var userUploadedFeedMessagesLocation = '../img/upload/feed/';

        var uniqueRandomImageName            = 'image-' + uniqueSHA1String;
        // This variable is actually an array which has 5 values,
        // The [1] value is the real image extension
        var imageTypeDetected                = imageBuffer

        var userUploadedImagePath            = userUploadedFeedMessagesLocation + 
                                               uniqueRandomImageName +
                                               '.' + 

        // Save decoded binary image to disk
                                  console.log('DEBUG - feed:message: Saved to disk image attached by user:', userUploadedImagePath);
            console.log('ERROR:', error);

        console.log('ERROR:', error);

Discussion courtesy of: GANGSTA IRL

This recipe can be found in it's original form on Stack Over Flow

Node.js Recipes

责编内容by:Node.js Recipes (源链)。感谢您的支持!


Dynatrace OneAgent SDK for Node.js: Extend end-to-... Dynatrace provides extensive Node.js monitoring capabilities, including heap and process metrics, h...
Excel Module for NodeJS I want to create .xls .xlsx file from NodeJS but i can't find any stable module for do that. Is...
Nodejs cms 内容管理框架 DoraCMS2.0 部署教程... 1、下载代码。通过github check代码(请记住是2.0.0的分支),怎么下不再过多解释。 git clone
Instrumenting Your Node/Express Application: Viewi... This post is the follow-up to Instrumenting Your Node/Express Application . Here we will begin to e...
Node.js has a higher cognitive load than Java I'm going to preface this by saying that I'm a much bigger user of Java than I am of node. I do l...