上传附件成功后的返回值问题

本文介绍了一个使用Ajax实现的Excel文件上传功能,详细展示了HTML表单、JavaScript处理逻辑、Java后台处理流程及Struts配置等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、页面上传Excel:
<form id="uploadForm" enctype="multipart/form-data" method="post">
<table id="tb_east">
<tr>
<td>文件名:</td>
<td><input id="fileName" class="srk"/></td>
</tr>
<tr>
<td>本地上传:</td>
<td><input type="file" id="file" name="upload"
title="请选择一个Excel文件"
onChange="if(this.value) getFileName(this.value);"/>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<a href="javascript:void(0);" id="btn"
class="easyui-linkbutton" onclick="uploadSubmit()">上传</a>
</td>
</tr>
</table>
</form>
<script type="text/javascript">
// 获取附件名
function getFileName(tValue){
var t1 = tValue.lastIndexOf("\\");
var t2 = tValue.lastIndexOf(".");
var suffix=tValue.substring(t2+1);
if (suffix!="xls") {
var file=document.getElementById("file");
if (file.outerHTML) { // for IE, Opera, Safari, Chrome
file.outerHTML = file.outerHTML;
} else { // FF(包括3.5)
file.value = "";
}
$("#fileName").val("");
alert("只支持后缀是.xls的Excel文件!");
return false;
}
if(t1 < t2 && t1 < tValue.length){
document.getElementById("fileName").value = tValue.substring(t1 + 1, t2);
}
}
// 上传Excel
function uploadSubmit(){
var isIE=0;
if (window.attachEvent) {
isIE=1;
}
var options = {
//跳转到相应的Action
url : "/ccit_datacenter/siphondata/upload.actionfunction=readTable&isIE="+isIE,
type : "POST",//提交方式
success : function(msg) {//调用Action后返回过来的数据
if (msg) {
$.messager.show({
title:"提示",
msg:"上传成功!",
showType:"show",
style:{left:"",right:0,
top:document.body.scrollTop+document.documentElement.scrollTop,
bottom:""}
});
}
msg=msg.replace(new RegExp(/(=)/g),":");
var data=eval("(" + msg+")");
$("#tables").combobox({
valueField: 'label',
textField: 'value',
data:data,
onSelect:function(record){
loadField(record);
}
});
}
};
$("#uploadForm").ajaxSubmit(options);//绑定页面中form表单的id
}
// 加载字段
function loadField(record){
var isIE=0;
if (window.attachEvent) {
isIE=1;
}
var options = {
//跳转到相应的Action
url : "/ccit_datacenter/siphondata/upload.actionfunction=readField&isIE="+isIE
+"&tableName="+record.value,
type : "POST",//提交方式
success : function(msg) {//调用Action后返回过来的数据
msg=msg.replace(new RegExp(/(=)/g),":");
var data=eval("(" + msg+")");
$("#centerGrid").datagrid("loadData",data);
}
};
$("#uploadForm").ajaxSubmit(options);//绑定页面中form表单的id
}
</script>
2、Action中处理完附件
/**
* 读取Excel文件
* @return
*/
public String upload(){
String result="";
try {
if(upload!=null){
Workbook book = Workbook.getWorkbook(upload);
Sheet[] tables=book.getSheets();
List<Map<String, Object>> tablesList=new ArrayList<Map<String, Object>>();
for (Sheet sheet2 : tables) {
Map<String, Object> tableMap=new HashMap<String, Object>();
tableMap.put("label", "'"+sheet2.getName()+"'");
tableMap.put("value", "'"+sheet2.getName()+"'");
tablesList.add(tableMap);
}
result=tablesList.toString();
book.close();
}
if (isIE==1) {// 是IE浏览器
is = new ByteArrayInputStream(result.getBytes("GBK"));
}else{
is = new ByteArrayInputStream(result.getBytes("UTF-8"));
}
} catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
}
3、struts.xml:
<action name="upload"
class="com.milestone.ccit.datacenter.action.ExcelUploadAction"
method="upload">
<result type="stream" name="success">
<param name="contentType">text/html</param>
<param name="inputName">is</param>
</result>
</action>
总结:用Ajax提交表单时,直接返回json={...},页面中根本接不到,故Struts中配置时,直接用流写回去的。返回到页面的都是字符串类型,可以用下面的形式转化为json对象:
msg=msg.replace(new RegExp(/(=)/g),":");// 把map组成的键值对的"="号转化为":"
var data=eval("(" + msg+")");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值