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