This is the form html code. I am using enctype="application/x-www-form-urlencoded"
currently.
<form name="{{this.status}}" class="commonForm" enctype="application/x-www-form-urlencoded" action="/api/upload/{{this.commonID}}" method="post">
<td>
<input type="file" id="{{this._id}}" class="form-control" name="FileUploadForClient" multiple required {{this.statusDisabled}} />
</td>
<td>
<button type="submit" class="btn btn-primary btn-block col-sm-2" style="font-size:16px" {{this.statusDisabled}}>Submit</button>
</td>
</form>
MY AJAX call:
var formData = new FormData(this);
formData.append('text',"Awesome Font!");
$.ajax({
type: "POST",
url: $(this).attr('action'),
data: formData,
processData: false,
contentType: false,
dataType: "json",
success: function(response){
if (response.status == '200') {
$.LoadingOverlay("hide");
swal({
title: "Excellent!",
text: "Files submitted successfully!",
icon: "success",
button: "Ok",
showCancelButton: true
}).then((result) => {
if (result) {
window.location.reload();
}
});
I want to send the text field which I have appended above along with the files uploaded.
Files are submitted successfully but when i try to use req.body.text
in nodejs
it gives undefined. WHy?????????????
Here is the route code:
router.post('/api/upload/:cid',function(req,res,next){
console.log("yes it came under api");
console.log("req.body.text = " + req.body.text + req.query.text);
upload2(req,res,function(err) {
if(err) {
console.log("Error is important = "+ err);
}
else
{
console.log("Uploaded");
}
})
})
So my main problem is: How to access the text field on server side in nodejs
after sending via AJAX
with FormData
??? Pls help!!!