How to upload file in MEAN Stack
Do these steps
1. npm install ng-file-upload
2. include ng-file-upload.min.js in your angular index .html
3. Use this example to copy form to your angular page from where u want to upload file. -- http://jsfiddle.net/0m29o9k7/
or http://jsfiddle.net/danialfarid/2vq88rfs/136/
Copy this code outside of any form which is already there:
4. Change this url from example code to where you want to upload files -- url: 'https://angular-file-upload-cors-srv.appspot.com/upload',
6. In your server.js or app.js which ever you are using as (node server.js) to start app add these lines
var crypto = require('crypto');
var mime = require('mime');
var multer = require('multer');
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'app/app-content/images/')
},
filename: function (req, file, cb) {
crypto.pseudoRandomBytes(16, function (err, raw) {
cb(null, raw.toString('hex') + Date.now() + '.' + mime.extension(file.mimetype));
});
}
});
var upload = multer({ storage: storage });
// make '/app' default route
app.post('/', upload.any(), function (req, res) {
res.send(req.files);
});
Change 'app/app-content/images/' this where you want your uploaded file will be
This code points your file upload url to index of your node server.
then you will be able to see the uploaded file.
Do these steps
1. npm install ng-file-upload
2. include ng-file-upload.min.js in your angular index .html
3. Use this example to copy form to your angular page from where u want to upload file. -- http://jsfiddle.net/0m29o9k7/
or http://jsfiddle.net/danialfarid/2vq88rfs/136/
Copy this code outside of any form which is already there:
4. Change this url from example code to where you want to upload files -- url: 'https://angular-file-upload-cors-srv.appspot.com/upload',
6. In your server.js or app.js which ever you are using as (node server.js) to start app add these lines
var crypto = require('crypto');
var mime = require('mime');
var multer = require('multer');
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'app/app-content/images/')
},
filename: function (req, file, cb) {
crypto.pseudoRandomBytes(16, function (err, raw) {
cb(null, raw.toString('hex') + Date.now() + '.' + mime.extension(file.mimetype));
});
}
});
var upload = multer({ storage: storage });
// make '/app' default route
app.post('/', upload.any(), function (req, res) {
res.send(req.files);
});
Change 'app/app-content/images/' this where you want your uploaded file will be
This code points your file upload url to index of your node server.
then you will be able to see the uploaded file.
Thank you for posting very valuable information.
ReplyDeleteMEAN stack Online Training
i love this very much very usefull for us
ReplyDeleteFull Stack Training in Chennai | Certification | Online Training Course | Full Stack Training in Bangalore | Certification | Online Training Course | Full Stack Training in Hyderabad | Certification | Online Training Course | Full Stack Training in Pune | Certification | Online Training Course | Full Stack Training | Certification | Full Stack Online Training Course
Very Nice Blog…Thanks for sharing this information with us. Here am sharing some information about training institute.
ReplyDeletetableau online training in hyderabad