0

What is the simplest way to encode PNG images to base64 using NodeJS? I need to store the presentation as a string in mongodb. The UI posts a mulitpart form and I can see the content being post correctly over the wire i.e;

Content-Disposition: form-data; name="userFile"; filename="Untitled2.png" Content-Type: image/png
Content-Type    multipart/form-data; boundary=---------------------------71011331421746

Need to iterate through a file list and encode each file.

app.post('/api/upload', function (req, res) {    

    var thumbnails = [].concat(req.files.thumbnail);

     for (var x = 0; x < thumbnails.length; x++) {
        taskItem.push({bin: new Buffer(thumbnails[x]).toString('base64')});
     }
}

Any help is appreciated.

J

user1859465
  • 883
  • 1
  • 11
  • 28
  • This answer may help you: http://stackoverflow.com/a/20285053/2145980 – Josh Burgess Nov 24 '14 at 16:22
  • So what is going wrong with your example code? – loganfsmyth Nov 24 '14 at 16:41
  • @JoshBurgess and @loganfsmyth, thanks for your help. 1) would like to handle the conversion to base64 on the server side. I had a simple example working passing up the string value from ` var dataURL = canvas.toDataURL(file.mimetype);`. As regards the server side logic, just looking for a nice way to handle the encoding per file. – user1859465 Nov 24 '14 at 17:10

0 Answers0