Reputation: 315
I'm uploading a file from my webpage to my nodejs server, which already works. The file is inside a form, along with some other input values, that I cannot find how to read.
HTML
<form class="modal-content" method="POST" action="http://localhost:3000/testupload" enctype="multipart/form-data" >
<span class="close">×</span>
<div class="modal-body">
<input type="file" value="Upload file" name="file" id="input-upload">
<input type="checkbox" value="Automatic Transcription" name="TESTCHECK">
<div>
<input type="checkbox" value="Automatic subtitling">
<select name="testSelect">
<option value="en-us">US English</option>
</select>
</div>
</div>
<div class="modal-footer">
<button >Upload Media</button>
</div>
</form>
nodejs
app.post("/testupload", (request, response, next) =>{
let form = new formidable.IncomingForm();
form.parse(request, function(err, fields, files){
//console.log(fields);
console.log(files.file.name); //Correctly gets the filename
//further file upload code
});
console.log(request);
response.end();
});
When I POST the form, I can get the file input back, but can't find the other inputs&values anywhere in the request.
Upvotes: 0
Views: 41
Reputation: 1794
According to the doc you can use events:
form.on('field', function(name, value) {
});
or
you could also reference directly via fields (as in the example on https://www.npmjs.com/package/formidable)
form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end(util.inspect({fields: fields, files: files}));
});
From what I understand you need to correctly name the form fields (first try with a simple example, one input text field).
Upvotes: 1