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

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+")");

猜你喜欢

转载自z-408.iteye.com/blog/1954700