Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save freidamachoi/7814880 to your computer and use it in GitHub Desktop.
Save freidamachoi/7814880 to your computer and use it in GitHub Desktop.
Image Upload - MEAN
/**
* Module dependencies
*/
var express = require('express');
var fs = require('fs');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
// img path
var imgPath = '/path/to/some/img.png';
// connect to mongo
mongoose.connect('localhost', 'testing_storeImg');
// example schema
var schema = new Schema({
img: { data: Buffer, contentType: String }
});
// our model
var A = mongoose.model('A', schema);
mongoose.connection.on('open', function () {
console.error('mongo is open');
// empty the collection
A.remove(function (err) {
if (err) throw err;
console.error('removed old docs');
// store an img in binary in mongo
var a = new A;
a.img.data = fs.readFileSync(imgPath);
a.img.contentType = 'image/png';
a.save(function (err, a) {
if (err) throw err;
console.error('saved img to mongo');
// start a demo server
var server = express.createServer();
server.get('/', function (req, res, next) {
A.findById(a, function (err, doc) {
if (err) return next(err);
res.contentType(doc.img.contentType);
res.send(doc.img.data);
});
});
server.on('close', function () {
console.error('dropping db');
mongoose.connection.db.dropDatabase(function () {
console.error('closing db connection');
mongoose.connection.close();
});
});
server.listen(3333, function (err) {
var address = server.address();
console.error('server listening on http://%s:%d', address.address, address.port);
console.error('press CTRL+C to exit');
});
process.on('SIGINT', function () {
server.close();
});
});
});
});
@stonnedd
Copy link

hi im new in mongoose so i have a basic questions i already save some image with your code but how can i display the image in ejs view
my code:

//  schema************************************************/

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var productSchema   = new Schema({

    idProduct: {
        type: mongoose.Schema.Types.ObjectId,
        index: true
    },
    category: {type: String},
    product: {type: String},
    model: {type: String},
    brand: {type: String},
    size: {type: String},
    price: {type: String},
    description: {type: String},
    color : {type: String},
    quantity: {type: Number},
    img1: { data: Buffer, contentType: String },
    uploadDate: {type: Date, default: Date.now}
});
module.exports = productSchema;

// model *****************************************/
var mongoose = require('mongoose');
var ProductSchema = require('../schemas/productSchema.js');

var Product = mongoose.model('Product',ProductSchema);
module.exports = Product;

// adding the data ****************************************/

var imgPath = './public/images/test.jpg';

var product = new productModel({
    category: 'something',
    product: 'something',
    model: 'something',
    brand: 'something',
    size:  'something',
    price: 'something',
    description :'something',
    color : 'something'
});

product.img1.data = fs.readFileSync(imgPath);
product.img1.contentType = 'jpg';
product.save(function(err, products) {
    if (err) return console.error(err);
    console.dir(products);

// routes/index.js **********************/

router.get('/', function(req, res) {
    productModel.find({}, function(err,products){
    res.render('index',{ products: products});
     });
});

// view  index.ejs*************/

<!DOCTYPE html>
<html>

<a href = "index">
    <% products.forEach(function(product){ %>
        <img src=<%=product.img1%> >
    <% }) %>
    <span>test</span>
</a>

</html>

what im doing wrong? it displays just the box of the image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment