5

Im uploading images with connect form. But it does not work if I use the bodyParser(). And the other way around, if i don't use the bodyParser, i cant upload files?

How do I make them play together? Here is my config:

app.configure(function() {
    app.register('.html', require('ejs'));
    app.set('views', __dirname + '/../views');
    app.set('view engine', 'html');
    app.use(gzippo.staticGzip(__dirname + '/../public'),{ maxAge: 86400000 });
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(form({ 
        keepExtensions: true,
        uploadDir: __dirname + '/../tmp'
    }));
    app.use(express.cookieParser());
    app.use(express.session({
        secret: 'test',
        cookie: { secure: true },
        store: new MySQLSessionStore(client.database, client.user, client.password)
    }));
    app.use(expressValidator);
    app.use(app.router);
    app.use(express.csrf());
});
georgesamper
  • 4,989
  • 5
  • 40
  • 59

1 Answers1

5

If you are using the latest Express you don't need to include connect-form (which is deprecated since Connect 1.8.x).

Just use req.files in your routes to get the uploaded files, Express does the rest. Check out this post:

http://tjholowaychuk.com/post/12943975936/connect-1-8-0-multipart-support

alessioalex
  • 62,577
  • 16
  • 155
  • 122
  • Oh thats nice :) But now i have a problem with the file name. With `req.files.image.filename` i get the name of the image before it was uploaded. And it´s renamed to a random string or something after. Where is that set? Can't seem to find anything on the subject? – georgesamper Dec 13 '11 at 17:25
  • You get the name and the path for the image. Just move that image from the temporary location into the desired one and you're good to go :) – alessioalex Dec 13 '11 at 17:44
  • The link is broken now, would be better to put the answer in here rather than pasting a link in – Mark Chorley Jan 06 '16 at 10:51
  • The link is now http://tjholowaychuk.tumblr.com/post/12943975936/connect-180-multipart-support but I think that whole approach is now out of date, search node.js express multipart. I ended up using multer – Astra Bear Mar 02 '16 at 02:55