5

I have small images I want to store as a bin data item. The form is posted but I do not know what to do in the controller so that if I so this, the data is stored. I am using Sails.js and the form is passing data as "image" with the form having a file input type.

var image = new Images;
image.data = ????
image.save();
Travis Webb
  • 14,688
  • 7
  • 55
  • 109
latitudehopper
  • 735
  • 2
  • 7
  • 23

1 Answers1

4

If the images are smaller than 16Mb, you can save them directly using MongoDB's bindata type. You can always convert the binary stream to a Base64 string, and store it as a string file, but that will reduce the allowed image size.

If the image is larger than 16Mb, you have no choice but to use GridFS.

Pickels
  • 86
  • 3
  • Thanks for the reply. That is exactly what I want to do. How do I convert the post input to base64 with node? – latitudehopper Apr 16 '15 at 19:59
  • You can create a base64 string using a Buffer. Take a look a this article: http://www.hacksparrow.com/base64-encoding-decoding-in-node-js.html – Pickels Apr 17 '15 at 04:12
  • 2
    Can somebody please share any link containing documentation/example of uploading file via bindata type? – Adil Sep 05 '17 at 07:34
  • 2
    Can you show us how it's done using MongoDB (and without using GridFS)? – DaveIdito Sep 10 '19 at 15:56