首页 > 编程语言 >JQuery ajax上传多个文件 java

JQuery ajax上传多个文件 java

时间:2022-10-26 11:23:08浏览次数:82  
标签:JQuery files false uploadFile formData ajax let file java

HTML

<div id="divImg">
   <input type="file" class="file" name="file" multiple="multiple" id="file"/>
   <button type="button" onclick="uploadFile()">上传</button>
</div>

JS

function uploadFile(){
        let files = $("#file").prop("files");
        let formData = new FormData();
        // 对每个文件进行循环处理
        for (let i = 0; i < files.length; i++) {
            let file = files[i];
            // 添加文件到formData
            formData.append('files['+i+']', file, file.name);
        }
        $.ajax({
            url: ctx + "/file/upload",
            type: 'POST',
            data: formData,
            processData: false,
            contentType: false,
            async: false,
            cache: false,
            success: function(data) {
                console.log(data);
            }
        });
    }

JAVA


    @PostMapping("/upload")
    @ResponseBody
    public List uploadFile(MultipartHttpServletRequest request){
        Map<String, MultipartFile> fileMap = request.getFileMap();
//        return fileService.uploadFile(files);
        return new ArrayList<>();
    }

转自CSDN

标签:JQuery,files,false,uploadFile,formData,ajax,let,file,java
From: https://www.cnblogs.com/strongerPian/p/16827616.html

相关文章