0

I'm getting the image data client-side (base64) from JavaScript via the FileReader() object, and then I post that data to the Node.JS/Express server and write an image via the following code:

fs.writeFile(__dirname+'/../resources/img/products/0.png', imageData, 'base64', function(err) {
    if(err) return res.send({err: 'There was an error saving product image'});
    res.send({});
});

but the image comes up as corrupt. A shortened version of the data looks like this:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZkAAAEpCAYAAABBb1+7AAAACXBIW…KxrrQ4WeEi84KFqYztjvTRlkNvrolLq7JzPEcwKtOIyP8PvUG8/X++RRoAAAAASUVORK5CYII=

Also, I split that data with (',') and only upload the [1] of the array, so only this part is sent to the server:

iVBORw0KGgoAAAANSUhEUgAAAZkAAAEpCAYAAABBb1+7AAAACXBIW…KxrrQ4WeEi84KFqYztjvTRlkNvrolLq7JzPEcwKtOIyP8PvUG8/X++RRoAAAAASUVORK5CYII=

EDIT: For the person who posted as possible dupe, using their code I get the variable 'matches' as null.

Leon Adler
  • 2,993
  • 1
  • 29
  • 42
Jake Cross
  • 523
  • 1
  • 6
  • 14
  • Possible duplicate of [NodeJS write base64 image-file](http://stackoverflow.com/questions/20267939/nodejs-write-base64-image-file) – Gaurav Dave Dec 05 '15 at 07:50
  • Can you provide the actual full valid base64? The one you provided is shortened with … in the middle, so obviously that one would be corrupt, but your code seems to otherwise correctly write the base64-decoded contents of imageData. You may want to log imageData just before writing to check that it actually contains what you think it does. – jcaron Dec 05 '15 at 12:10

0 Answers0