Extjs上传文件到数据库

前端:
var uploadForm=Ext.create('Ext.form.Panel', {
region: 'center',
bodyStyle : 'padding: 0px 0px 0 0px; background-color: #DFE8F6',
margin: '0 0 0 0',
xtype: 'form',
method : 'POST',
fileUpload: true,
split: true,
//autoScroll:true,
//collapsible: true,
layout : {
type : 'table',
columns: 1
},
bodyStyle: 'background:#DFE8F6; ',
fieldDefaults: {
labelAlign: 'right',
labelWidth: 45,
width:300,
msgTarget: 'side',
padding:10
},
items: [
{
id:"question.questionId_upload",
xtype:"textfield",
name: 'question.questionId',
hidden:true
},
{
//xtype:'filefield',
id:'upload',
inputType : "file",
xtype : 'textfield',
emptyText: '请选择附件',
//allowBlank: false,
fieldLabel : '附件',
buttonText:"浏览",
name : 'file'
}
],
buttons: [
{
text: '确定',
listeners : {
click : function(btn, e, eOpts) {
if (uploadForm.getForm().isValid()) {
Ext.Msg.confirm("请确认?","确定上传?", function(button,text){
if(button=='yes'){
Ext.yh.form.submit({"form":uploadForm,
"url": basePath+'/system/uploadFile.action',
success : function(options_) {
//成功
//alert("----"+options_.result.entity.contentStr);
Ext.getCmp("question.questionId").setValue(options_.result.entity.questionId);
Ext.getCmp("question.questionId_upload").setValue(options_.result.entity.questionId);
win_2.hide();
},
failure : function(options_) {
//失败
win_2.hide();
}
});
}else{
return false;
}
});

}
}
}
},
{
text:"关闭",
xtype:'button',
handler:function(){
win_2.hide();
}
}
]
});
注意事项:panel中必须设置为fileUpload: true
后台Action:
/**
* 上传文件
* @return
*/
public String saveQuestion() throws Exception{
//try {
result = new ExtResult();
System.out.println(file);
if(file!=null){
question.setFileName(fileFileName);
String fileType[]=fileFileName.split("\\.");
question.setFileType(fileType[1]);
question.setQuestionFiles(getBytesFromFile(file));
}
questionService.saveOrUpdate(this.getUser(), question);
result.setEntity(question);
result.setMsg(Constants.OPT_SUCCESS);
//} catch (Exception e) {
// TODO: handle exception
//erro(e);
//}
return SUCCESS;
}
配置文件XML:


result
text/html;charset=utf-8
null

你可能感兴趣的:(Extjs,structs2,spring)