2

I did researches about storing images in a mongodb database using meteor and I found this code and I get blocked in it before storing in the database:

var fs = Npm.require('fs');

// function to encode file data to base64 encoded string
function base64_encode(file) {
  // read binary data
  var bitmap = fs.readFileSync(file);
  // convert binary data to base64 encoded string
  return new Buffer(bitmap).toString('base64');
}

  // convert image to base64 encoded string
  var base64str = base64_encode('Chrysanthemum.jpg');
  console.log(base64str);

The problem is that Npm.require('fs'); doesn't work in the client side. If you have a solution for this or another solution such as a plugin working in meteor with a bar progress (for multiple images using bindata) on how to store images in mongodb, please help me. Thank you in advance.

Stennie
  • 63,885
  • 14
  • 149
  • 175
Abdelouhab
  • 179
  • 3
  • 13

2 Answers2

2

Most common approach would be to use CollectionFS to store data in Mongo using their built in GridFS feature. This will also let you work around their 16mb document size limit. And provide set of various useful helper functions in client and server side.

Martins Untals
  • 2,128
  • 2
  • 18
  • 31
  • 2
    For future searchers, remember that you don't *need* to use GridFS if your document will be less than 16mb. You can just store it as a base64 string and (even better) with the BinData type: [related question](http://stackoverflow.com/questions/11442356/storing-some-small-under-1mb-files-with-mongodb-in-nodejs-without-gridfs) (still learning about this stuff myself) –  Mar 30 '17 at 13:32
  • 1
    Also see [this comment](http://stackoverflow.com/questions/3413115/is-gridfs-fast-and-reliable-enough-for-production/5627933#comment48539493_14675806): "Even smaller files would not benefit to be stored with GridFS. If your file could be stored in a MongoDB document (i.e. < of its 16 MB size limit), you would rather store the file as a BLOB within a MongoDB document. It will by-pass the overhead of using GridFS on top of MongoDB storage. See compose.io/articles/gridfs-and-mongodb-pros-and-cons" -- This post also has some good info: http://menge.io/2015/03/24/storing-small-images-in-mongodb/ –  Mar 30 '17 at 14:44
  • If you can predict that size will always be smaller - then sure. Btw CollectionFS is not maintained anymore. There are better alternatives now – Martins Untals Mar 30 '17 at 15:29
0
const mongoose = require("mongoose");
const { Schema } = mongoose;
const fs = require('fs');
const path = require('path');

let uri = "mongodb://localhost:27017/testBin";
mongoose.connect(uri, {
    useUnifiedTopology: true,
    useCreateIndex: true,
    useNewUrlParser: true
}).then(async(db) => {
    console.log("connected success");
    const blogSchema = new Schema({
        file: { type: Buffer }
    }, { strict: false });
    const Blog = mongoose.model('mycollection', blogSchema, "mycollection");
    console.log("path.resolve('./index.js') ", path.resolve(__dirname, 'index.js'));
    const file = fs.readFileSync(path.resolve(__dirname, 'index.js'))
    await new Blog({ file }).save()
    mongoose.connection.close()

}).catch(err => {
    console.log(err);
})
  • 2
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Tyler2P Jan 06 '21 at 12:34