Can I process the fields first and then process the files in Nodejs busboy
Asked Answered
V

2

8

I am using the Busboy to upload the form data which contains the file and some text fields.

Every thing is working fine i am able to get the post parameters and file.

How can i achieve this: First I need to process the fields data and save in Db and process the file and update in same record in DB.

The busboy is first process the file and then process the fields.

req.pipe(req.busboy);
req.busboy.on('file', function (fieldname, file, filename) {
    console.log("Uploading: " + filename);
    console.log("fieldname: "+fieldname);
});
req.busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated) {
    var jsondata = JSON.parse(val);
});

Any suggestions

Venatic answered 6/1, 2015 at 13:4 Comment(0)
C
10

Simple: just put your fields before your files in your form. Example:

<form>
  <input type="text" name="foo">
  <input type="text" name="bar">
  <input type="file" name="baz">
</form>
Conchology answered 6/1, 2015 at 15:25 Comment(3)
Can't it be control from the server side?Venatic
No, the client is the one that orders the request data.Conchology
I don't see how the async module would help you with this particular problem.Conchology
A
1

busboy will read the data in the order which we append formData

form.append('field',value);
form.appnd('file',file);
Almagest answered 20/9, 2020 at 11:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.