Hey. I am working on this simple form, trying to send a file to my Nodejs server using FormData, but for some reason Node never gets it. Also, how can I Node send back a message on the confirmation page that the file was received. What am I doing wrong or missing? Please help. Thank you in advance. Here is my code.
HTML
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
var fd = new FormData();
fd.append( 'file', input.files[0] );
$.ajax({
url: '/uploadfile',
data: fd,
processData: false,
contentType: false,
type: 'POST',
success: function(data){
alert(data);
}
});
</script>
</head>
<body>
<form enctype='multipart/form-data'>
<input type= "text" name = "theName"/>
<input type="file" id="file" name="file">
<input type="submit">
</form>
</body>
</html>
Server (Nodejs)
var express = require('express');
var router = express.Router();
router.get('/', function(req, res){
res.sendfile('./public/html/form-file.html');
});
router.post('/', function(req , res){
console.log('Request received: ');
console.log(req.body)
console.log('\nRequest recieved: \nmethod: ' + req.method + '\nurl: ' + req.url)
res.end('callback(\'{\"msg\": \"OK\"}\')');
});
module.exports = router;
source
share