sql >> Database >  >> NoSQL >> MongoDB

hoe de afbeelding naar de map te uploaden met nodejs en het pad op te slaan in mongodb?

Je kunt nodejs formidable en fs-extra module gebruiken om een ​​map te maken om afbeeldingen in je applicatie op te slaan.Probeer dit

var fs = require('fs-extra');
var formidable = require('formidable');
var EmpDetail   = require('../models/employee.model.js');//This line means that I am having my schema code.
var    util = require('util');


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

res.json({success:true});

var empData = new EmpDetail();//Here I created object to call my schema values.

var form   =  new formidable.IncomingForm();
console.log(req.body,'req.body');
form.parse(req,function(err,fields,files){
    console.log('dfgbfbf');
    util.inspect({fields: fields, files: files});
});

form.on('field',function(name,value){//This means your html input fields
    console.log('fields',name,value);
    if(name == 'fullName'){
        console.log('here',value);
        empData.fullName = value;
    }
    if(name == 'fatherName'){
        empData.fatherName = value;
    }
    if(name == 'dt'){
        empData.DOB = value;
    }
});
form.on('file',function(field, file){//This means on click of file it gets that file.
    console.log(file.name,'hjgjg');
    var temp = file.path;
    console.log(temp,'temp',file.name);
    var fileName = file.name;
        var fileName = file.name;
        var location     = 'images/';
        var cpLocation   = 'empDetails/images/';
        empData.imgSize = file.size;
        empData.imgType = file.type;
        empData.picFile = location+fileName;//Here PicFile is name of Avatar in model
fs.copy(temp,cpLocation+fileName ,function(err){//This means it create a folder and name of the image in your app.
        if(err)
        {
            console.log(err);
        }
    });

});
form.on('end',function(fields, files){

        empData.save(function(err,resobj){
            if(err)
            {    
               throw err;
            }
        });
});
});


  1. Stel Redis-cachevoorvoegselsleutel in op Symfony

  2. Hybride cloud versus volledige openbare cloud - voor- en nadelen

  3. Robomongo-verbinding met Meteor mongodb

  4. MongoDB-project bijgewerkt record in een geneste array in findAndModify-query