javascript - Multer returning req.file undefined -
hey, know question has been asked plenty of times before, , i've tried implementing solutions, don't work me.
i have been tearing hair out trying figure out how upload file , read file size through node. tried using formidable npm, seems no longer maintained can't find documentation on it. had no way of dealing errors tried using multer. however, repeatedly undefined log when try log req.file.
i have server.js code below
var express = require('express'); var formidable = require('formidable'); var multer = require('multer'); var path = require('path'); var upload = multer({dest: './uploads'}); var fs = require('fs'); var app = express(); var port = 8080; app.use(express.static(__dirname+'/views')); app.set('views', './views'); app.set('view engine', 'jade'); app.get('/', function(req, res){ res.render('index.jade'); }); app.post('/upload', upload.single('upload'),function(req, res){ console.log(req.file); }); app.listen(port, function(){ console.log('express listening on port: '+port); });
my javascript code ajax call provided below
$('#upload-butt').on('change', function(){ var file = $(this).get(0).files; console.log(typeof file); if(file.length > 0){ var formdata = new formdata(); formdata.append('upload', file, file.name); $.ajax({ url: '/upload', type: 'post', data:formdata, processdata:false, contenttype:false, error: function(jxhr, status){ console.log('error: '+status); }, success: function(data){ console.log('upload successful: '+data); } }) } });
my index.jade code given below
html head link(rel='stylesheet', href='style.css', type='text/css') title upload file shortening body h1 welcome file metadata service div(id='upload-button') form(enctype='multipart/form-data', method='post', action='/upload') input(name='upload', type='file', id='upload-butt') div(id="submit-button") form(action = '/submit') button(type="submit", value='submit', id='submit-butt') submit script(src="https://code.jquery.com/jquery-2.2.0.min.js") script(src="upload.js")
i ready tear hair out, grateful can me here! thanks!
you have not applied middle correctly.
you can use this:
var express = require('express') var multer = require('multer') var app = express() app.use(multer({ dest: './uploads/'})) can access fields , files in request object: console.log(req.body) console.log(req.files)
so in ur code have apply
var express = require('express'); var formidable = require('formidable'); var multer = require('multer'); var path = require('path'); var upload = multer({dest: './uploads'}); var fs = require('fs'); var app = express(); var port = 8080; app.use(upload); app.use(express.static(__dirname+'/views')); app.set('views', './views'); app.set('view engine', 'jade'); app.get('/', function(req, res){ res.render('index.jade'); }); app.post('/upload', upload.single('upload'),function(req, res){ console.log(req.file); }); app.listen(port, function(){ console.log('express listening on port: '+port); });
Comments
Post a Comment