我正在尝试使用filefield
ExtJS 上传图像文件并将其作为form
服务器端(“Express”)提交。我在下面粘贴了我的代码:
我尝试为 req.body 引用 API,但req.body
总是undefined
. 请让我知道我在哪里做错了。
外部 JS 代码
{
xtype: 'form',
name:'imageForm',
items:[{
xtype:'textfield',
name:'name',
value:'Krisna'
},{
xtype: 'filefield',
hidden: false,
buttonOnly:true,
buttonText:'Change Photo',
name: 'imageUrl',
//bind: '{currentContact.imageUrl}',
listeners:{
change:function(fielField, value){
var filePath = value;
fileNameIndex = filePath.lastIndexOf("\\");
fileName = filePath.substring(fileNameIndex + 1);
fileExt = fileName.substring(fileName.lastIndexOf(".") + 1);
var form = this.up('form').getForm(); // working
if(fileExt === 'jpeg' || fileExt === 'jpg' || fileExt === 'gif' || fileExt === 'png'){
if (form.isValid()) {
form.headers = {
'Content-Type' : 'application/json;charset=utf-8',
"Accept" : "application/json;charset=utf-8"
};
form.submit({
url : 'http://localhost:3000/changePhoto',
waitMsg : 'Uploading your Photo...',
success : function (fp, o) {
Ext.Msg.alert('Success', 'Your Photo has been uploaded.');
//Write code here to set the server side image url so that that image gets displayed , but not as fakepath.
},
failure : function (fp, o) {
Ext.Msg.alert('Failed', 'Your Photo failed to uploaded.');
}
});
}
}
else if(value){
//showOKInfoMsg(SWP_Msg.SWP_CompressFileFrmtMsg);
Ext.Msg.alert('Warning', 'Please select Image file only');
this.reset();
return true;
}
}
}
}]
}
快递代码:index.js:
var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');
router.use(bodyParser.json()); // for parsing application/json
router.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
//var multer = require('multer'); // tried using multer, but I think this is only for file upload not form submit
//var temp = multer();
//router.use(bodyParser());
//router.use(multer()); // If I try adding multer at this line it is throwing error
router.post('/changePhoto', function (req, res) {
console.log(req.body); // Outputting {} i.e., empty object
var userName = req.body.name; // coming undefined as req.body is undefined
res.json(req.body); // sending back {}
});
module.exports = router;
注:Express 版本:“express”:“~4.13.1”