文件上传 通过隐藏iframe的方式来实现ajax上传文件并返回处理结果 ie乱码问题解决

1:生成隐藏iframe用来接收上传文件时的返回值
	/**文件上传*/
	$("#upload_btn_id").click(function(){
		var form = $("#upload_form_id")[0]; 
		if(!($("#tempFileFrame")[0])){
			var tempFrame = document.createElement("iframe");
			tempFrame.id="tempFileFrame";
			tempFrame.name="tempFileFrame";
			tempFrame.style.display="none";
			document.body.appendChild(tempFrame);
		}
		form.target="tempFileFrame";
		document.getElementById("tempFileFrame").target="_self";
		form.action="media/upload_adVideo.action";
		form.submit();
	});

action中的处理
httpResponse.setCharacterEncoding("utf-8");
		// 返回给客户端的json对象
		JSONObject jsonObject = new JSONObject();

httpResponse.getWriter().print("<script type='text/javascript'>window.parent.result(\'" + jsonObject + "\')</script>" + "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />");注意在chrome和ff中不需要加<meta>但是为了兼容ie不出乱码需要加上meta而且为了加上后chrome不出问题必须把<meta>放在<script>后面(否则chorme中<meta>和<script>放以子串的形式放到body中而不是head中),具体原因不清楚,此乃测试结果。


返回结果解析:
/**文件上传结果分析*/
function result(msg){
	$("#upload_btn_id").attr("disabled",false);
	//返回的json对象
	if(msg != null && msg !=""){
		var msg = eval("(" + msg + ")");
		if(msg.outFilelimit == "yes"){
			//文件大小超出范围
			$("#result").html("<font color='red'>文件大小超出范围100M,上传失败</font>");
		}else if(msg.loginFtp == "no"){
			$("#result").html("<font color='red'>登录ftp失败,上传文件失败</font>");
		}else if(msg.uploadSuccess == "yes"){
			$("#result").html("<font color='green'>上传文件成功</font>");
			$("#upLoadSuccess_input_id").val("yes");
			//反填页面信息
			$("#fileFullAddr_input_id").val(msg.fileFullAddr);
			$("#filename_input_id").val(msg.filename);
			$("#videoLenth_input_id").val(msg.videoLenth);
			$("#videoType_select_id").val(msg.videoType);
			$("#upload_btn_id").attr("disabled",true);
		}else if(msg.uploadSuccess == "no"){
			$("#result").html("<font color='red'>上传文件失败</font>");
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值