SpringBoot 文件上傳

在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(這里是限制的文件大小)

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容