zoukankan      html  css  js  c++  java
  • input file multiple 配合springmvc实现多文件上传

    1、前端页面的样子
    
    <input id="file" name="file" type="file" multiple="multiple"  />
    2、前端的js代码
    
    var formData = new FormData(); 
                var files = $("#file")[0].files;
                for(var i = 0 ; i < files.length ; i ++){
                    formData.append('file',files[i]);
                }
    3、ajax的代码
    $.ajax({
                       url: '${cpath}/biz/xxxx/addFile',
                        type: 'POST',
                        cache: false,
                        data: formData,
                        processData: false,
                        contentType: false
                }).done(function(res) {
                    debugger;
                }); 
    4、后端的代码
    
    @RequestMapping(value = "addFile", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
        @ResponseBody
        public String addFile(HttpServletRequest request,HttpServletResponse response) {
            
            String rRoles = request.getParameter("rRoles");
            List<MultipartFile> fileList = new ArrayList<>();
            //创建一个通用的多部分解析器 
            CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
            //判断 request 是否有文件上传,即多部分请求  
            if(multipartResolver.isMultipart(request)){
                //转换成多部分request    
                MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;  
                //取得request中的所有文件名  
                fileList = multiRequest.getFiles("file");
                if (fileList == null || fileList.size() <= 0) {
                    throw new Exception("上传文件失败");
                }
                System.out.println("");
            }
            return null;
        }
    5、xml配置文件配置
        <bean id="multipartResolver"  
            class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
            <!-- set the max upload size100MB -->  
            <property name="maxUploadSize">  
                <value>104857600</value>  
            </property>  
            <property name="maxInMemorySize">  
                <value>4096</value>  
            </property>  
        </bean> 
  • 相关阅读:
    QT常用技巧--程序延时
    python中zip()函数的用法
    numpy.random.choice(a, size = None, replace = True, p = None)用法
    Python keras.layers .ZeroPadding2D() 详解
    Socat 入门笔记
    echo命令的使用
    Type Error('keyword argument not understood: ', 'interpolation') 解决方案
    Pytorch 包下载
    双边滤波Matlab代码
    hihocoder 第170周 Word Construction (dfs+剪枝)
  • 原文地址:https://www.cnblogs.com/Kendy/p/10966256.html
Copyright © 2011-2022 走看看