使用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>

相关推荐