日期:2014-05-16 浏览次数:20627 次
<head> <script type="text/javascript"> function ajax_callback(result) { if(/^1:/.test(result)){ alert(result.substr(3)); }else{ window.location.href=result; } return; } function submitform(){ var form=document.getElementById('fm'); form.target="check_frame"; form.submit(); } </script> </head> <body> <form action="uploadImg" enctype="multipart/form-data" method="post" id="fm" onsubmit="return false"> ... ... <input class="fileBtn" name="realFile1" id="realFile1" type="file"/> ... ... <a href="javascript:void(0)" class="button" onclick="return submitform()">提交</a> ... ... <iframe name="check_frame" style="display:none;"></iframe> </form> </body>
public String appendJS(String res) { String prefix = "<script type=\"text/javascript\" language=\"javascript\">document.domain=\"yourdomain.com\";parent.ajax_callback('"; String suffix = "')</script>"; return res == null ? prefix + suffix : prefix + res + suffix; } public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { boolean verify = false; ... ... ... ... //当文件上传上来之后,先进行文件后缀名,文件大小的服务端验证 //如果不符合,就使用下面的代码返回到前端 if(!verify){ HttpServletResponse response = inv.getResponse(); response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String jsRes = appendJS(res); if(logger.isDebugEnabled()) logger.debug("jsRes -> " + jsRes); out.println(jsRes); return; } }