我正在向我的Node.js服务器发送带有ajax的图像文件。当我想观看文件数据时,它显示我'未定义'。
app.js:
var express = require("express");
var app = express();
var server = require('http').createServer(app);
var path = require('path');
var bodyParser = require('body-parser');
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use(express.static(path.join(__dirname, 'includes')));
app.use(bodyParser.urlencoded({ extended: false }));
app.post('/image-upload', function(req, res){
console.log(req.files); ////// undefined
});
var port = process.env.PORT || 3000;
server.listen(port);
html:
<form id="uploadimage" action="" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file"/>
<input type="submit" value="Upload" id="submit-stock" />
</form>
javascript:
$("#uploadimage").on('submit',(function(e) {
e.preventDefault();
$.ajax({
url: base_url + "/image-upload",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData: false
});
}));