JimCoder05
JimCoder05

Reputation: 33

When sending FormData with a file and field, busboy only fires the file event

I am sending FormData with a file and field to a route on my server but am having trouble getting connect-busboy to fire both events.

On the client side I have:

var data = new FormData();
data.append('file', MyFile);
data.append('key_one', 'value_one');

I am using fetch so then I assign data to the body property of the fetch object.

fetch(url, {

method: 'POST',
body: data

}).then(...){...}.catch(...){...}

On the server url route:

I use the busboy middleware configured with

limits: {fileSize: 5 * 1024 * 1024, parts: 2}

Then I have,

req.pipe(req.busboy);

req.busboy.on('field', function(key, value){
    console.log('field fired');
}

req.busboy.on('file', function(fieldname, file, filename){
    console.log('file fired');
}

My problem is that I only get 'file fired'. If I take out the file listener then I will get 'field fired'. Does anyone know why only file gets fired with this set up? Also, the only other middlewares I am using right now are cookieParser, expressSession and passport.

Upvotes: 1

Views: 1571

Answers (1)

JimCoder05
JimCoder05

Reputation: 33

After taking another look at the documentation I realized that I needed to handle the readable stream. In my case "file" is the stream.

req.busboy.on('file', function(fieldname, file, filename){
    console.log('file fired');
    file.resume();
}

This fixed my problem.

Upvotes: 1

Related Questions