angularjs - Multer 1.0.3 Mean Stack file upload -
i trying upload multipart/form-data simple mean stack application. when putting in 1 file , running it works fine.
server.js
var express = require('express'); var multer = require('multer'); var storage = multer.diskstorage({ destination: function (req, file, cb) { cb(null, 'uploads/'); }, filename: function (req, file, cb) { switch(file.mimetype) { case 'image/jpg' : case 'image/jpeg': case 'image/gif': var extension = file.mimetype.split("/"); extension = extension[extension.length-1]; break case 'video/quicktime': var extension = 'mov'; break case 'video/mp4': var extension = 'mp4'; break default: var extension = 'jpeg'; } cb(null, file.fieldname + '-' + date.now() + "." + extension); } }); var upload = multer({ storage: storage }); var path = require('path'); var app = express(); var segmentupload = upload.fields([{ name: 'segmentvideo', maxcount: 1}, { name: 'segmentstill', maxcount: 1}, { name: 'segmentgif', maxcount: 1}]) app.post('/photos/upload', segmentupload, function (req, res, next) { console.log(req); console.log(req.files['segmentvideo'][0]); console.log(req.files['segmentstill'][0]); console.log(req.files['segmentgif'][0]); console.log(req.body.title); }); app.get('/', function(req, res) { res.sendfile('./index.html'); }); app.listen(8080); console.log("app listening on port 8080");
index.html
<body> <form action="/photos/upload" method="post" enctype="multipart/form-data"> title: <input type="text" name="title"> <br><br> select video upload: <input type="file" name="segmentvideo" id="filetoupload"> <br><br> select jpeg upload: <input type="file" name="segmentstill" id="filetoupload"><br><br> select gif upload: <input type="file" name="segmentgif" id="filetoupload"><br><br> <input type="submit" value="upload image" name="submit"> </form>
but when try integrate multer node routing cannot app accept multipart/form-data.
app/routes.js
var video = require('./models/video'); var multer = require('multer'); var storage = multer.diskstorage({ destination: function (req, file, cb) { cb(null, 'uploads/'); }, filename: function (req, file, cb) { switch(file.mimetype) { case 'image/jpg' : case 'image/jpeg': case 'image/gif': var extension = file.mimetype.split("/"); extension = extension[extension.length-1]; break case 'video/quicktime': var extension = 'mov'; break case 'video/mp4': var extension = 'mp4'; break default: var extension = 'jpeg'; } console.log("new extension: " + extension); cb(null, file.fieldname + '-' + date.now() + "." + extension); } }); var upload = multer({ storage: storage }); module.exports = function(app) { // api -------------------------------------- // todos app.get('/api/videos', function(req,res){ // use mongoose todos in database video.find(function(err, videos){ // if there error, send error if (err) res.send(err); res.json(videos); }); }); var segmentupload = upload.fields([{ name: 'segmentvideo', maxcount: 1}, { name: 'segmentstill', maxcount: 1}, { name: 'segmentgif', maxcount: 1}]) app.post('/api/videos', segmentupload, function(req, res){ // create video video.create({ title : req.body.title, description : req.body.description, category : req.body.category, day : req.body.day, videourl : req.body.videourl, stillurl : req.body.stillurl, gifurl : req.body.gifurl, airready : false }, function(err, video) { if (err) res.send(err) // , return todos after create video.find(function(err, videos){ if (err) res.send(err) res.json(videos); }); }); }); // delete todo app.delete('/api/videos/:video_id', function(req,res){ video.remove({ _id : req.params.video_id }, function(err, video) { if (err) res.send(err); // , return todos after delete 1 video.find(function(err, videos) { if (err) res.send(err) res.json(videos); }); }); }); // edit todo app.put('/api/videos/:video_id', function(req,res){ video.findbyidandupdate(req.params.video_id, { $set: { title: req.body.title }}, function(err, video){ if (err) res.send(err) // , return todos after edit video.find(function(err, videos){ if (err) res.send(err) res.json(videos); }); }); }); // find ------------------------------------------------------------- app.get('*', function(req, res) { res.sendfile('./public/index.html'); // load single view file (angular handle page changes on front-end) }); };
i receive error:
typeerror: cannot read property 'title' of undefined
this first post stack overflow, appreciate of support!
the problem might due use sending multipart/form-data
not converting json
, using json
retrieving data.
use body-parser
var bodyparser = require('body-parser'); var app = express(); app.use(bodyparser.json());
Comments
Post a Comment