使用AJAX上传文件及表单数据
如果想要通过ajax异步请求上传文件及数据,需要下载ajaxfileuplad.js,见附件。
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', data:data, fileElementId:arrId, success:function(datas){ if(datas.status==0){//成功后跳转到新的页面 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('导入失败是否下载错误模板'); 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{ //如果失败 返回错误模范下载路径 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="导入" 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" >选择导入模板:</span> <input type="file" id="uploadfile" name="uploadfile" size="50" value="" style="width:300px;" dataType="Require" labelText="导入模板"> </td> </tr>
相关推荐
kentrl 2020-11-10
结束数据方法的参数,该如何定义?-- 集合为自定义实体类中的结合属性,有几个实体类,改变下标就行了。<input id="add" type="button" value="新增visitor&quo
ajaxyan 2020-11-09
zndy0 2020-11-03
学留痕 2020-09-20
Richardxx 2020-11-09
learningever 2020-09-19
chongxiaocheng 2020-08-16
ajaxhe 2020-08-16
lyqdanang 2020-08-16
curiousL 2020-08-03
TONIYH 2020-07-22
时光如瑾雨微凉 2020-07-19
83510998 2020-07-18
坚持着执着 2020-07-16
jiaguoquan00 2020-07-07
李永毅 2020-07-05
坚持着执着 2020-07-05