javascript - req.body is Empty on angular file-upload, node js -
so i've been working on uploading excel file save database. form.
<form method="post" enctype="multipart/form-data"> <label class="uk-form-file md-btn md-btn-primary" for="user_upload">upload file</label> <input style="display:none;" type="file" ngf-select ng-model="user_datasource" name="userdatasource_upload" id="userdatasource_upload" accept=".xlsx,.xls,.csv" ngf-max-size="20mb" fd-input ng-change="upload"/> </form>
this controller handles upload
$scope.upload = function() { upload.upload({ url: '/api/sr/user_upload', data: { username: 'test', file: file_upload } }).then(function(response) { console.log(response); }); }
and node
app.use(function(req, res, next) { res.setheader("access-control-allow-methods", "post, put, options, delete, get"); res.header("access-control-allow-origin", "http://127.0.0.1:3000"); res.header("access-control-allow-headers", "origin, x-requested-with, content-type, accept"); next(); }); app.use(bodyparser.json()); app.use(bodyparser.urlencoded({ extended: true })); app.post('/api/sr/user_upload',function(req, res) { console.log('============================================,'); console.log(req); });
so can see. include everything. body-parser
, setheader
etc. no luck. req.body
empty. going upload file save database username. after username im going use multer
please me out! thanks!
as using multer on basis of writing answer
the multer saves files first, , writes
req
remaining part of form - can hidden field.
so need rest data in upload
function this
upload(req, res, function(err) { if (err) { console.log(err); return res.end('error'); } else { console.log(req.body); req.files.foreach(function(item) { console.log(item); // move file destination }); res.end('file uploaded destination'); } });
hope work you
Comments
Post a Comment