-
Notifications
You must be signed in to change notification settings - Fork 259
Closed
Labels
Description
I developed all my project with bodyparser but recently, someone asked me to add a way to upload some files. But I didn't manage to make your project work with mine. I tried something like this :
var express = require('express');
var session = require('express-session');
var hbs = require('hbs');
var bodyParser = require('body-parser');
var fileUpload = require('express-fileupload');
var app = express();
app.set('view engine', 'html');
app.engine('html', hbs.__express);
app.use('/', express.static('public'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(session({
secret: 'azerty',
resave: false,
saveUninitialized: false
}));
app.use(fileUpload());
app.post('/upload', function(req, res) {
console.log(req.files);
if (!req.files) {
res.send('No files were uploaded.');
return;
}
});
with a form generated this way :
var form = $("<form></form>")
.attr("method", "POST")
.attr("action", "upload")
.attr("encType", "multipart/form-data")
.attr("ref", "uploadForm")
.attr("id", "uploadForm")
.attr("postaction", "refreshMenu()")
.addClass("formulaire");
form.append($("<table></table>")
.css("text-align", "center")
.append($("<tr></tr>")
.append($("<td></td>").html("Choisissez un fichier : "))
.append($("<td></td>")
.append($("<input/>")
.attr("type", "file")
.attr("name", "fichier")
)
)
)
.append($("<tr></tr>")
.append($("<td></td>")
.attr("colspan", "2")
.append($("<input/>")
.attr("type", "submit")
.attr("value", "Upload!")
)
)
)
).dialog({width: 650, title: "Uploader un fichier", dialogClass: "no-close"});
but when I try to submit my form req.files is always undefined as tells me console.log(req.files); in my server-side code... could you please explain me why ? I expect bodyparser is blocking multipart requests but if it is actually, is there a way to workaround ? And if yes, how ?
y3nd and MrToph