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>"); } } }
文件上传 通过隐藏iframe的方式来实现ajax上传文件并返回处理结果 ie乱码问题解决
原文:http://blog.csdn.net/chx10051413/article/details/18559345