使用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>
server.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); 剛剛返回{}
你可以發佈你的app.js文件代碼嗎? – abdulbarik
@abdulbarik var bodyParser = require('body-parser'); var busboy = require('connect-busboy'); app.use(busboy()); app.use(bodyParser()); 我在app.js中使用middelware – user3046493