Skip to content

How do I use it with bodyparser #11

@thedwarf21

Description

@thedwarf21

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 ?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions