ajax部分
$.ajax({
url: '/article/create',
type: 'POST',
data: formData,//<----要传输的数据
async: false,
cache: false,
timeout:100,
contentType: false,//<----头信息设置为false
processData: false,
success: function(data){
alert("文件上传成功");
},
error: function(xhr){
alert('文件上传失败:原因是' + xhr.status);
}
});//end ajax
express代码
var multer = require('multer')
var storage = multer.diskStorage({
destination: function (req, file, cb){
cb(null, './public/upload')
},
filename: function (req, file, cb){
cb(null, file.originalname)
}
});
var uploadsingle = multer({
storage: storage
});
exports.savePost = function (req,res,next) {
// console.log(req.files); 打印出来是undefined
var upload=uploadsingle.single('file');
upload(req, res, function (err) {
// console.log(req);
console.log('ok');
console.log(err);
next()
})
npm install --save multer