我使用busbody在nodejs中上传文件我可以上传文件但我无法在bodyparser中获取文本消息我的fornt结束代码是:
<script>
$("input[type='button']").click(function(){
var files = $("input[type='file']").get();
var formData = new FormData();
var text = "xxxxx";
var url = "fileUpload";
if (files.length > 0){
for (var i = 0; i < files.length; i++) {
var input = $("input[type='file']").get(i).files;
var file = input;
if(file.length>0){
console.log( file[0].name);
formData.append('userpic',file[0], file[0].name);
}
}
formData.append('text',text);
$.ajax({
type: "post",
url: url,
processData: false,
contentType: false,
data: formData, // serializes the form's elements.
});
}
});
</script>
服务器.js
router.post('/fileUpload', function(req, res) {
console.log(req.body.text);
// req.pipe(req.busboy);
req.busboy.on('file', function(fieldname, file, filename) {
console.log('ok2');
var fstream = fs.createWriteStream('./public/images/' + filename);
file.pipe(fstream);
fstream.on('close', function () {
});
});
res.send('upload succeeded!');
});
这行代码 console.log(req.body.text); 只需返回 {}