为什么把form的target设置成iframe的name了提交表单后仍然是全部刷新?

有两个问题
一是,把form的target设置成iframe的name了提交表单后仍然是全部刷新
二是,在后台处理了之后,在java中怎么发送信息到前台去?在下面的用response前端收不到任何信息。

html代码

<form id="subdata" method="post" action="${ctx}/position/getPaperFile.do" target="iUpload" enctype="multipart/form-data" style="display:none">
        <input type="file" name="file">
</form>
<iframe name="iUpload"></iframe>

JS代码

$("#subdata").submit();

后台java代码

public void getPaperFile(MultipartFile file,HttpServletResponse res) throws IOException{
        PrintWriter writer=res.getWriter();
        if (null != file && null != file.getOriginalFilename() && !"".equals(file.getOriginalFilename())){
            String fileName = file.getOriginalFilename();//上传的文件名
            String[] nameArr = fileName.split("\\.");
            String suff = nameArr[nameArr.length-1];
            fileName = "file" + UUID.randomUUID() +"."+suff;
            String path = WebApplication.getSession().getServletContext().getRealPath("../uploadFile/paper/");
            File targetFile = new File(path, fileName);  
            if(!targetFile.exists()){  
                targetFile.mkdirs();  
            } 
            glo_path=path;
            filepath="../uploadFile/paper/" + fileName;
            fname=fileName;
            //保存  
            //paper.setPaperFile("../uploadFile/paper/" + fileName);
            try {  
                file.transferTo(targetFile);  
                writer.write("<script type='text/javascript' type='language'>parent.alert('上传成功');</scrpt>");
            } catch (Exception e) {  
                writer.write("<script type='text/javascript' type='language'>parent.alert('文件错误');</scrpt>");
                throw new RuntimeException("文件保存异常"+e.getMessage()); 
            }    
        }
    }
阅读 5.8k
3 个回答

题主可以参考下我的代码(你的缺少submit按钮?或者……我也不造是什么原因……):

<div id="upload_div" class="hide">
    <form id="upload_form" enctype="multipart/form-data" action="API/V1/file_upload_callback.php?callback=upload_callback" target="iframe_hide" method="POST">
        <input id="upload_file" type="file" name="file" />
        <input id="upload_submit" type="submit" />
    </form>
    <iframe name="iframe_hide" frameborder="0" height="0" width="0" marginheight="0" marginwidth="0"></iframe>
</div>

后端返回的数据类似这样:

<script type="text/javascript">window.parent.upload_callback('{"result":"success","data":"http://115.159.106.238/CloudDisk/1/A (1).jpg","description":"文件上传成功"}');</script>

目测是因为你写的scrpt,是script啊,卧槽!

既然都jquery了,那就用jquery的form插件吧

推荐问题
宣传栏