前提
React TypeScript の axiosで、SpringBootにMultiPartFileをPostしたのち、
Springのローカルに保存したいのですが、
そもそもファイルが受け取れません。
今回は .png
.jpg
のいずれかの送信を想定しています。
発生している問題・エラーメッセージ
org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present
該当のソースコード
tsx
1import axios from "axios"; 2 3const PostImage = function () { 4 function onclick() { 5 const file: any = document.getElementById('file'); 6 console.log(file.files[0]) 7 8 const formData = new FormData() 9 formData.append('file', file) 10 const headers = { "content-type": "multipart/form-data" }; 11 axios.post('/postimage', formData, { headers })//FormDataパターン 12 axios.post('/postimage', { file: file }, { headers })//辞書パターン 13 } 14 return ( 15 <> 16 <input type="file" id="file" /> 17 <button onClick={onclick}>post</button> 18 </> 19 ) 20}; 21 22export default PostImage;
java
1@PostMapping("/postimage") 2public void postimage( 3 @RequestPart("file") MultipartFile file 4// @RequestParam("file") MultipartFile file 5) { 6 System.out.println(file.getName()); 7}
試したこと
以上のFormDataでPost、辞書でPost、
@RequestPartで受け取り、@RequestParamで受け取り
いずれの組み合わせでも同じ 'file' is not present
で怒られます
補足情報(FW/ツールのバージョンなど)
json
1"version": "0.1.0" 2"dependencies": { 3 "axios": "^0.27.2", 4 ... 5}
xml
1<modelVersion>4.0.0</modelVersion> 2<parent> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-parent</artifactId> 5 <version>2.7.3</version>

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。