網站首頁 編程語言 正文
想做一個文件上傳,spring boot配合Ajax來進行。卻報錯:Current request is not a multipart request
這是錯誤截圖:
當時發生這種錯誤,我是很震驚的,我以為找了很多辦法來解決。
有以下辦法,當然這些并未解決我的問題,但是部分有用,就先列舉出來:
1.在頁面頭部加入信息:
<meta http-equiv="Content-Type" content="multipart/form-data; charset=utf-8" />
此方法 未解決 問題!
2.在form表單加入屬性:enctype
<form method="post" enctype="multipart/form-data"> <input type="file" name="file" /> </form>
此方法 未解決 問題!
3.后端:@RequestParam MultipartFile file 改為 @RequestPart MultipartFile file
此方法 未解決 問題!
還有個別方法。。。都不行
下面是解決方法,我之前的ajax代碼:
$('#upload-ok').click(function () {
var form = document.getElementById("upload-form");
var file = new FormData(form);
$.ajax({
url: "/addFile",
type: "POST",
processData:false,
contentType:false,
data:{
"file":file
},
success: function (date) {
xxxxxx
},
error: function (date) {
xxxxxx
}
})
})
正確代碼:
$('#upload-ok').click(function () {
var form = document.getElementById("upload-form");
var file = new FormData(form);
$.ajax({
url: "/addFile",
type: "POST",
processData:false,
contentType:false,
//重要部分,data的傳的是整個表單,不用大括號包裹;不用自定義變量名
data:file,
success: function (date) {
xxxxxx
},
error: function (date) {
xxxxxx
}
})
})
就這樣,解決!!!
原文鏈接:https://blog.csdn.net/qfxl0724/article/details/125215386
相關推薦
- 2022-10-14 修改全局 element-ui 元素
- 2022-04-18 python?numpy中對ndarry按照index增刪改查_python
- 2022-06-07 FreeRTOS實時操作系統結構示例_操作系統
- 2022-04-19 Android線性布局與相對布局的實現_Android
- 2022-07-30 react中的事件處理
- 2022-07-18 Kotlin 正確退出 foreach、foreachIndexed 循環函數
- 2022-05-10 react使用require圖片無法加載
- 2023-07-29 [plugin:vite:import-analysis]Failed to resolve imp
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支