这是一个上传文件的对话框:var uploadForm = new Ext.form.FormPanel({
labelAlign: 'right',
title: 'Upload Model',
heigth : 100,
frame:true,
fileUpload: true,
items: [
{xtype:'textfield',
id : 'description',
name : 'description',
fieldLabel:'Description',
width : 200
},
{xtype : "textfield",
fieldLabel: 'Select Upload File',
id:'file',
name: 'file',
height : 22,
width : 265,
allowBlank : false,
itemCls:'float-left',
clearCls : 'allow-float',
inputType: 'file'},
new Ext.Button({
text: 'Upload',
handler: function() {
var filePath = Ext.getCmp("file").getValue();
var fileType = filePath.substr(filePath.length - 4,4);
if(fileType.toLowerCase() != ".jpg" && fileType.toLowerCase() != ".gif"){
Ext.Msg.alert("Tips","Just jpg or gif File!");
return;
}
uploadForm.getForm().submit({
method : 'post',
url : 'update/AddPic.action',
success: function(form, action){
Ext.Msg.alert('Message', 'success');
},
failure: function(){
Ext.Msg.alert('Error', 'faile');
}
});
}
})
]
});
var createwindow = new Ext.Window({
title : 'Form Upload',
width : 415,
height : 390,
closable : true,
items : uploadForm
});
createwindow.show();已经通过URL跳到action里面了,服务器端的代码怎么写才能使图片上传到服务端呢??
能不能给具体点的代码,之前在网上找了很久都取不到上传的图片...
labelAlign: 'right',
title: 'Upload Model',
heigth : 100,
frame:true,
fileUpload: true,
items: [
{xtype:'textfield',
id : 'description',
name : 'description',
fieldLabel:'Description',
width : 200
},
{xtype : "textfield",
fieldLabel: 'Select Upload File',
id:'file',
name: 'file',
height : 22,
width : 265,
allowBlank : false,
itemCls:'float-left',
clearCls : 'allow-float',
inputType: 'file'},
new Ext.Button({
text: 'Upload',
handler: function() {
var filePath = Ext.getCmp("file").getValue();
var fileType = filePath.substr(filePath.length - 4,4);
if(fileType.toLowerCase() != ".jpg" && fileType.toLowerCase() != ".gif"){
Ext.Msg.alert("Tips","Just jpg or gif File!");
return;
}
uploadForm.getForm().submit({
method : 'post',
url : 'update/AddPic.action',
success: function(form, action){
Ext.Msg.alert('Message', 'success');
},
failure: function(){
Ext.Msg.alert('Error', 'faile');
}
});
}
})
]
});
var createwindow = new Ext.Window({
title : 'Form Upload',
width : 415,
height : 390,
closable : true,
items : uploadForm
});
createwindow.show();已经通过URL跳到action里面了,服务器端的代码怎么写才能使图片上传到服务端呢??
能不能给具体点的代码,之前在网上找了很久都取不到上传的图片...
呵呵,一时考虑不周,我是用jsp的.
回复全部:
昨晚改了之后前台代码是这样的:var fm = new Ext.FormPanel({
title: 'Upload File',
url:'update/AddPic.action?t=' + new Date(),
autoScroll:true,
region:'center',
height: 120,
width: 300,
frame:false,
fileUpload: true,
defaultType:'textfield',
labelWidth:200,
items:[{
xtype:'field',
fieldLabel:'Please Select File to Upload',
allowBlank:false,
inputType:'file',
name:'file'
}],
buttons: [{
text: 'Start Upload',
handler: function(){
if(fm.form.isValid()){//验证form, 本例略掉了
//form提交
fm.getForm().submit();
}
else{
Ext.Msg.alert("Message","Please Select File!");
}
}
}]
});
var createwindow = new Ext.Window({
title : 'Form Upload',
width : 415,
height : 250,
closable : true,
layout:'border',
items : fm
});
createwindow.show();
后台的代码如下: HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory); //upload.setProgressListener(new MyProgressListener(request));
List items = upload.parseRequest(request);//重点看这一句,取不出文件的信息 Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) { } else {
//String fieldName = item.getFieldName();
String fileName = item.getName();
//String contentType = item.getContentType();
System.out.println();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
File uploadedFile = new File(uploadPath/*保存的路径*/
+ System.currentTimeMillis()
+ fileName.substring(fileName.lastIndexOf(".")));
item.write(uploadedFile);
}
}request不是null,而且里面的文件信息也在debug的时候能看出来,但是就是不能将其取出来置于upload变量里面,items是null