Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

How do I use it with bodyparser #11

Closed
thedwarf21 opened this issue Oct 19, 2016 · 5 comments
Closed

How do I use it with bodyparser #11

thedwarf21 opened this issue Oct 19, 2016 · 5 comments

Comments

@thedwarf21
Copy link

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 ?

@thedwarf21
Copy link
Author

I found as a solution not to use bodyparser at all... I will have to rewrite most of my code :-(

@richardgirges
Copy link
Owner

I thought this issue occurred if bodyParser middleware was called out after express-fileupload. But based on your issue that's not the case.

I'm going to look into this and will keep you posted.

@dmolineuxcb
Copy link

I'm having a similar issue with this, when I define fileUpload() before the app.use bodyParser, then req.files does always work as expected, however I have found that for some reason application/x-www-form-urlencoded requests always fail, I suspect this has something to do with having busboy on streams, although I could be completely wrong about this.

When I define it after the bodyParser, then I have no problem with application/x-www-form-urlencoded requests, but I have found that req.files is undefined.

I'm not sure if this is of much help to you, but I have made this small node application that reproduces the failure when application/x-www-form-urlencoded request comes in and fileUpload() is defined before the bodyParser.

I am using node version v7.2.1

repro_onabort.zip

@dmolineuxcb
Copy link

Sorry I have actually just found that you knew about this exact problem here: #9

@r3wt
Copy link
Contributor

r3wt commented Feb 7, 2017

just pulled this from a project i'm working on:

app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.use(fileUpload({
	limits: { 
		fileSize: 1 * 1024 * 1024,
		fields: 50,
		files: 1,
		parts: 51,
	}
}));

it works for me

This was referenced Nov 18, 2019
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

4 participants