Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm having trouble getting the file recognized server side with node.

Specifically when accessing request data, (req.files or req.body)

Here are some snippets if someone has any advice.

html:

<form action="/upload" class="dropzone" drop-zone id="file-dropzone"></form>

angular controller:

'use strict';

angular.module('app')
  .controller('SampleCtrl', function ($scope, $http) {


  })

  .directive('dropZone', function() {
    return function(scope, element, attrs) {
     $(element).dropzone({ 
        url: "/upload",
        maxFilesize: 100,
        maxThumbnailFilesize: 5
    });
  }
});

node router:

'use strict';

var express = require('express');
var controller = require('./import.controller');

var router = express.Router();

router.post('/', controller.import);

module.exports = router;

node controller:

'use strict';

var express = require('express'),
    _ = require('lodash'),
    fs = require('fs'),
    path = require('path'),
    multer = require('multer'),
    bodyParser = require('body-parser'),
    app = express();

app.use(bodyParser.urlencoded({extended: true}));

exports.import = function(req, res) {
    console.log(req.files); // responds with 'undefined'
    console.log(req.body); // responds with {}
};

thanks in advance

share|improve this question
    
You're adding the multer middleware to the app in your controller. I don't think it is doing what you expect it to as exports.import has no connection to app. –  Jordonias 22 hours ago

1 Answer 1

You have to actually use the file upload middleware to get express to unpack that part of the request. From the multer docs:

var express = require('express')
var multer  = require('multer')

var app = express()
app.use(multer({ dest: './uploads/'}));

console.log(req.body)
console.log(req.files)
share|improve this answer
    
thanks for the reply. unfortunately adding app.use(multer({ dest: './uploads/'})); still gives me the same responses. –  user1554903 yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.