0

I am trying to perform file upload using formidable module. I followed their example and it works well, when i do the same using express module, i dont get it working. It hungs there, after the log i kept in POST /upload.

This is the link for sample. https://github.com/felixge/node-formidable/blob/master/example/upload.js

I think it is something to do with the 'uploadDir' value. I have given current directory('/') as the directory lookup to express, and i expected the files uploaded to be present in files folder in the current directory, from where node is run. Donno where it is getting stuck ? any help is appreciated...!! Thanks,

var formidable = require('formidable'),
http = require('http'),
util = require('util');
connectTimeout = require('connect-timeout');
mongoose = require('mongoose');
express = require('express');
app = express();
server = http.createServer(app);

server.listen(4000);

app.configure(function(){
app.set('views', __dirname + '/views');
app.use(express.favicon());
app.use(connectTimeout({ time: 20000 }));
app.use(express.methodOverride());
app.use(express.bodyParser());
app.use(express.static(__dirname + '/'));
});

app.get('/',function(req, res) {

res.writeHead(200, {'content-type': 'text/html'});
res.end(
'<form action="/upload" enctype="multipart/form-data" method="post">'+
'<input type="text" name="title"><br>'+
'<input type="file" name="upload" multiple="multiple"><br>'+
'<input type="submit" value="Upload">'+
'</form>'
);
});

app.post('/upload', function(req,res) {

console.log(" ########## POST /uplaod ####### "+ req.files+" :: "+__dirname);
var form = new formidable.IncomingForm(),
files = [],
fields = [];
form.uploadDir = __dirname+"/files";

form.on('field', function(field, value) {
    fields.push([field, value]);
})
form.on('file', function(field, file) {
    console.log(file.name);
    files.push([field, file]);
})
form.on('end', function() {
    console.log('done');
    res.redirect('/forms');
});
form.parse(req);

});
1

1 Answer 1

1

You need to comment express.bodyParser() to make it work. ;)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.