zoukankan      html  css  js  c++  java
  • 【JavaWeb】ajax异步文件上传

    1. html部分

        <form id="uploadForm" enctype="multipart/form-data">
            <input type="file" name="file" /> <input type="file" name="file" />
            <input type="button" value="上传" id="upload" />
        </form>

    2. js部分

    $(document).ready(function(){
            $("#upload").click(function(){
                $.ajax({
                url: 'upload.do',
                     type: 'POST',
                     cache: false,
                     data:new FormData($('#uploadForm')[0]),//h5的DataForm对象
                     dataType:"json",
                     processData: false,
                     contentType: false,
                     success:function(data){
                        alert(data.status);
                     }
                })  
            })
        })

    3. java部分

    @ResponseBody
        @RequestMapping("upload.do")
        public String upload(String un,@RequestParam("file") MultipartFile[] file, HttpServletRequest req)
                throws IllegalStateException, IOException {
            String basePath = "C:\WebFile\yao2san\";
            boolean isSuccess = false;
            for (MultipartFile f : file) {
                String path = "";
                if (!f.isEmpty()) {
                    if (f.getOriginalFilename().endsWith("txt"))
                        path = basePath + "doc"; 
                    else if (f.getOriginalFilename().endsWith("jpg") || f.getOriginalFilename().endsWith("gif")
                            || f.getOriginalFilename().endsWith("png"))
                        path = basePath + "img";
                    else
                        path = basePath + "other";
                    String fileName = f.getOriginalFilename();
                    File tarFile = new File(path, fileName);
                    f.transferTo(tarFile);
                    isSuccess = true;
                }
            }
            return "{"status":"+isSuccess+"}";
        }
  • 相关阅读:
    VS快捷键
    eclipse快捷键(shift+ctrl+l能出来所有的快捷键)
    永远不要觉得自己代码有多6
    winform中使用webBrowser时如何与JS交互
    HTML CSS
    HTTP 协议 session cookie
    [Python3]Python官方文档-Python Manuals
    [python3]PyCharm编辑器
    Loadrunner上传文件与下载文件脚本
    Spotlight安装
  • 原文地址:https://www.cnblogs.com/cnsec/p/13286759.html
Copyright © 2011-2022 走看看