Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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 Dec 5 '14 at 20:36
    
Hi, I have the exact same issue, did you manage to make it work? – Egidi Feb 15 '16 at 19:41

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 Dec 5 '14 at 18:42

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.