node.js - store files in mongodb using mongoose -


i new node , mongodb. developing app nodejs, express , mongodb. want read csv/xlsx file file input field , store in mongodb using mongoose. having difficulties. using angularjs in front end. can give me suggestions procedure should go through? specific code great help.

i used busboy module store files in specific folder. here code

in routes:

router.post('/fileupload', function (req, res) {     var fstream;     req.pipe(req.busboy);     console.log(req.pipe);     console.log(req.busboy);     req.busboy.on('file', function (fieldname, file, filename) {         console.log("uploading: " + filename);         fstream = fs.createwritestream('./files/' + filename);         file.pipe(fstream);         fstream.on('close', function () {             res.redirect('back');         });     }); }); 

and frontend:

<form method="post" action="/fileupload" enctype="multipart/form-data">     <input type="file" id="file" name="file">     <button type="submit">submit</button> </form> 

up there no error. want store these files in database. should next?

you need understand basis storing files in mongo. why files stored in mongo , how stored?

from have done, need mongoose plugin store uploaded file mongo gridfs. gridfs here:- http://docs.mongodb.org/manual/core/gridfs/. can use compatible driver access grid - mongoose , mongoose plugin gridfs-stream example - see here: https://www.npmjs.org/package/gridfs-stream

i have used below save file gridfs.

var express = require('express'); var formidable = require('formidable'); var mongoose = require('mongoose'); var grid = require('gridfs-stream'); var fs = require('fs'); var util = require('util'); var app = express();  app.post('/fileupload', function (req, res) {     var form = new formidable.incomingform();     form.uploaddir = __dirname + "/data";     form.keepextensions = true;     form.parse(req, function(err, fields, files) {         if (!err) {           console.log('file uploaded : ' + files.file.path);           grid.mongo = mongoose.mongo;           var conn = mongoose.createconnection('..mongo connection string..');           conn.once('open', function () {           var gfs = grid(conn.db);           var writestream = gfs.createwritestream({               filename: files.file.name           });           fs.createreadstream(files.file.path).pipe(writestream);        });      }            });    form.on('end', function() {                res.send('completed ..... go , check fs.files & fs.chunks in  mongodb');    });  });  app.get('/', function(request, response){     response.send(         '<form method="post" action="/fileupload" enctype="multipart/form-data">'         + '<input type="file" id="file" name="file">'         + '<input type="submit" value="submit">'         + '</form>'         );     });  app.listen(40000, function() {     console.log('express listening on port 40000'); }); 

the above guide on how should proceed after uploading file , not production ready proof of concept. note replaced busboy formidable personal preference.

does move on?

so1


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -