关键处:
[color=red]1 (1) 服务器返回数据类型应设为text/html.
Response. setContentType("text/html; charset=utf-8");
(2) 返回字符串格式应有:
success部分,其后为false或true;
msg部份,其后是要返回的信息。
例1:服务器返回成功信息
{success:true,msg:'成功'}
例2:服务器返回失败信息
{success:false,msg:'失败'}
2 客户端
(1) Formpanel的fileUpload属性其值要设成true
(2) 表单提交回调的两个函数应为:
failure: function(form,action){},
success: function(form,action){} [/color]
-------------------------------------------------
后台struts2 action
服务端源码
用了apcher的 fileLoad组件
相关的两个包为:
commons-io-1.4.jar
commons-fileupload-1.2.1.jar
另外使用了spring的mvc框架。其他框架相应变动就行了
说明:spring返回的ModelAndView 视图必须是null,否则返回时会出现视图网页
而导制出现下载框
[color=red]1 (1) 服务器返回数据类型应设为text/html.
Response. setContentType("text/html; charset=utf-8");
(2) 返回字符串格式应有:
success部分,其后为false或true;
msg部份,其后是要返回的信息。
例1:服务器返回成功信息
{success:true,msg:'成功'}
例2:服务器返回失败信息
{success:false,msg:'失败'}
2 客户端
(1) Formpanel的fileUpload属性其值要设成true
(2) 表单提交回调的两个函数应为:
failure: function(form,action){},
success: function(form,action){} [/color]
-------------------------------------------------
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="resources/ext21/resources/css/ext-all.css" />
<link rel="stylesheet" type="text/css" href="resources/ext21/resources/css/fyhC.css" />
<script type="text/javascript" src="resources/ext21/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="resources/fckeditor/fckeditor.js"></script>
<script type="text/javascript" src="resources/ext21/ext-all.js"></script>
</head>
<script>
Ext.onReady( function (){
var form = new Ext.form.FormPanel({
labelAlign: 'right',
title: 'form',
labelWidth: 50,
frame:true,
fileUpload: true,
baseParams : {aa:'bb'}, //文件上传时,没有用?迷惑中
url:" login.do",
width: 380,
items: [{
xtype: 'textfield',
fieldLabel: '文本框',
name: 'file',
inputType: 'file'//文件类型
}],
buttons: [{
text: '按钮',
handler: s
}]
});
function s() {
form.getForm().submit({//客户端的数据提交给服务器
waitTitle:"请稍候",
waitMsg:"正在提交表单数据,请稍候。。。。。。",
//如果submit失敗,執行這一個function
failure:function(form1,action){
Ext.MessageBox.hide();
Ext.MessageBox.alert('Error',action.result.msg);
},
success: function(form1,action){
Ext.MessageBox.hide();
Ext.MessageBox.alert('Success',action.result.msg);
}
})
}
form.render(document.body);
});
</script>
<body>
<div id="hello"/>
</body>
</html>
后台struts2 action
package action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.FileItem;
/**
*
* @author cloud
*/
public class Login extends SimpleFormController{
public Login(){
// this.setCommandClass(Fuser.class);
}
private static final long serialVersionUID = 7440302204266787092L;
String uploadPath = "d:\\uploadtest\\"; // 用于存放上传文件的目录
String tempPath = "d:\\tmp\\"; // 用于存放临时文件的目录
@Override
protected ModelAndView onSubmit(HttpServletRequest arg0, HttpServletResponse arg1, Object cmd, BindException ex) throws Exception {
arg1.setContentType("text/html; charset=utf-8");
// arg1.setContentType("text/html; charset=ISO-8859-4");
ModelAndView aaa=null;
String data="{success:flase,msg:'失败'}";
// if ( !( arg0.getParameter("aa").equals("bb") ) ) {
// aaa=new ModelAndView(this.getFormView());
// arg1.getWriter().print("{success:flase,message:'失败1'}");
// return aaa;
// }
try {
java.io.File tmpfile=new java.io.File(tempPath);
boolean isMultipart = ServletFileUpload.isMultipartContent(arg0);
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096);//设置缓冲区大小,这里是4kb
factory.setRepository(tmpfile); // 设置临时目录
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(4194304);// 设置最大文件尺寸,这里是4MB
List fileItems = upload.parseRequest(arg0);// 得到所有的文件:
Iterator i = fileItems.iterator();
// 依次处理每一个文件:
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
String fileName = fi.getName();// 获得文件名,这个文件名包括路径:
if (fileName != null) {
// 在这里可以记录用户和文件信息
// 此处应该定义一个接口(CallBack),用于处理后事。
// 写入文件a.txt,你也可以从fileName中提取文件名:
String extfile = fileName.substring(fileName.indexOf("."));
Timestamp now = new Timestamp((new java.util.Date())
.getTime());
SimpleDateFormat fmt = new SimpleDateFormat(
"yyyyMMddHHmmssSSS");
String pfileName = fmt.format(now).toString().trim();
System.out.println(uploadPath + pfileName + extfile);
fi.write(new File(uploadPath + pfileName + extfile));
}
}
data="{success:true,msg:'上传成功'}";
} catch (Exception e) {
data="{success:flase,msg:'失败'}";
// 可以跳转出错页面
// aaa=new ModelAndView(this.getFormView());
} finally{
arg1.getWriter().write(data);
arg1.getWriter().flush();
return aaa;
}
}
}
服务端源码
用了apcher的 fileLoad组件
相关的两个包为:
commons-io-1.4.jar
commons-fileupload-1.2.1.jar
另外使用了spring的mvc框架。其他框架相应变动就行了
说明:spring返回的ModelAndView 视图必须是null,否则返回时会出现视图网页
而导制出现下载框