網站首頁 編程語言 正文
想做一個文件上傳,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-12-07 C++?IO設備讀寫功能實現詳解_C 語言
- 2022-05-18 Python中的turtle畫箭頭,矩形,五角星_python
- 2022-07-04 Python+Pillow+Pytesseract實現驗證碼識別_python
- 2022-10-11 kafka-報錯kafka.common.InconsistentClusterIdExceptio
- 2023-03-30 C語言對結構體數組按照某項規則進行排序的實現過程探究_C 語言
- 2022-07-13 python版jpeg合成pdf兩種方法
- 2022-04-10 MyBatis 查詢的時候屬性名和字段名不一致的問題
- 2022-08-28 go?zero微服務實戰處理每秒上萬次的下單請求_Golang
- 最近更新
-
- 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同步修改后的遠程分支