If you want to upload files and data through ajax asynchronous request, you need to download ajaxfileuplad.js, see the attachment.
function importNewTemplate() { var arrId = []; arrId[0] = "uploadfile"; var data = getFormJson("form"); var filePath = document.getElementById("uploadfile").value; $.ajaxFileUpload({ url:APP_PATH+"/yn/user/importNew.do?", async: false, type:'post', dataType:'json', date: date, fileElementId: arrId, success:function(datas){ if(datas.status==0){//Jump to a new page after success var rightTemplatePath = datas.data.rightTemplatePath; var optUserName = datas.data.optUserName; var submitUrl = APP_PATH+"/yn/user/submitTmp.do?rightTemplatePath=" +rightTemplatePath + "&order.optUser.name=" + optUserName; window.location.href = submitUrl; }else{ var result = confirm('Import failed to download the wrong template'); var downloadPath = datas.data.downloadPath; if(result){ var href = APP_PATH + downloadPath; window.open(href, "_blank"); } } } });}
public String importNew() throws Exception { String rtUserUuid = this.getRTUser().getUuid(); IUserAuthzService userService = (IUserAuthzService) com.ultrapower.iam.core.BeanFactory.getBean(com.ultrapower.iam.server.common.BeanIDConstants.USER_AUTHZ_FLOW); String realPath = System.getProperty("flowrealPath"); long time = System.currentTimeMillis(); realPath = realPath+"/download/user_template_new"+time+".xls"; String downloadPath = "/download/user_template_new"+time+".xls"; this.errorTemplatePath=downloadPath; File file = this.addAttachment (); ExcelOperation oo = new ExcelOperation (); oo.setFile(file); oo.setType("0"); oo.setPath(realPath); oo.setUserUUID (rtUserUuid); ExcelOperation excelOperation = userService.initExcelObj (oo); JsonResp jsonResp = JsonResp.asEmpty(); if (excelOperation.isFlag ()) { this.getResponse().setContentType("text/html;charset=UTF-8"); jsonResp.add("rightTemplatePath", this.rightTemplatePath); jsonResp.add("optUserName", this.getOrder().getOptUser().getName()); this.getResponse().getWriter().write(jsonResp.success().toJson()); return null; }else{ //If it fails, return the error model download path this.getResponse().setContentType("text/html;charset=UTF-8"); jsonResp.add("downloadPath", downloadPath); this.getResponse().getWriter().write(jsonResp.error("").toJson()); return null; } }
<td class="button_bar" colspan="2"> <input type="button" id="importNewTem" value="import" class="button_save" onmouseover="this.className='button_save_hover'" onmouseout="this.className='button_save'" onclick="importNewTemplate();"/> </td>
<tr> <td colspan="4" style="padding:3px 0;"> <span class="requiredFlg" >*</span> <span class="detailCurrent2" >Select import template:</span> <input type="file" id="uploadfile" name="uploadfile" size="50" value="" style="width:300px;" dataType="Require" labelText="导入模板"> </td> </tr>