req.busboy.on('file') not firing
Asked Answered
A

1

10

I have the following form:

form(method='post', action='/encoder_post', enctype='multipart/form-data')
    .form-group
        label(for='name') Name
        input.form-control(type='text', id='name', name='name')
    .form-group
        label(for='message') Message
        input.form-control(type='text', id='message', name='message')
    .form-group
        label(for='file') Audio File (*.wav)
        input.form-control(type='file', id='file')
    button.btn.btn-cicada.btn-block(type='submit') Submit

Inside encoder_post, I have the following function to handle the post request:

router.post('/', function(req, res){
    req.busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
        console.log("this is in file");
    });
    req.busboy.on('field', function(key, value, keyTruncated, valueTruncated) {
        console.log("The value is: " + value);
    });
    req.pipe(req.busboy);
});

However, whenever I submit the form, the 'field' handler triggers, but the 'file' doesn't.

Inside app.js I have:

var busboy = require('connect-busboy');
app.use(busboy());

Any ideas?

Abamp answered 2/7, 2015 at 13:38 Comment(4)
Are you sure busboy waits for your handler middle-ware to be reached to fire its parsing events ? I think those events should be handled right when busboy starts parsing request.Brooklynese
@S.D. By default the connect-busboy merely setups up an instance on req.busboy. It doesn't start parsing until you start writing to it.Enrichetta
@Enrichetta Ah yes. You need to pipe request data to it.Brooklynese
@S.D. Yep, he's already doing that. The problem though is a missing name attribute for the file field.Enrichetta
E
18

Your file field is missing a name attribute.

Enrichetta answered 2/7, 2015 at 13:50 Comment(2)
Just saved me about a weeks worth of debugging - thank youSump
Still struggling even after adding this attribute, wondering if it's because the app's in Angular and I might be missing something else?Cacogenics

© 2022 - 2024 — McMap. All rights reserved.