在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) => {
//上傳進(jìn)度
if (progressEvent.lengthComputable) {
var percentComplete = Math.round(progressEvent.loaded * 100 / progressEvent.total);
console.log(percentComplete);
}
}
}).then((response) => {
//....
})
上傳文件大小限制
springboot 默認(rèn)設(shè)置了上傳文件大小的限制
multipart.maxFileSize=50Mb(這里是限制的文件大小)
multipart.maxRequestSize=50Mb(這里是限制的文件大小)