在Controller中写入方法
@PostMapping("/save")
public String save(@RequestParam("file") MultipartFile file) {
//...
return null;
}
前端代码
html
<form>
<input type="file" id="file">
</form>
js
var formData = new FormData();
var file = document.getElementById('file1').files[0];
formData.append("file", file );
//ajax上传(使用axios)
axios.post('../save', formData, {
headers: {
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'multipart/form-data'
},
onUploadProgress: (progressEvent) => {
//上传进度
if (progressEvent.lengthComputable) {
var percentComplete = Math.round(progressEvent.loaded * 100 / progressEvent.total);
console.log(percentComplete);
}
}
}).then((response) => {
//....
})
上传文件大小限制
springboot 默认设置了上传文件大小的限制
multipart.maxFileSize=50Mb(这里是限制的文件大小)
multipart.maxRequestSize=50Mb(这里是限制的文件大小)