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.

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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 (still learning about this stuff myself)
Also see this comment: "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: menge.io/2015/03/24/storing-small-images-in-mongodb
If you can predict that size will always be smaller - then sure. Btw CollectionFS is not maintained anymore. There are better alternatives now
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); }) 

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.