我使用 Axios
和 'Content-Type': 'multipart/form-data'
以 POST 请求发送视频,但在 Spring 中我看到以下错误:
"[org.springframework.web.HttpMediaTypeNotSupportedException: Content type '' not supported]"
Axios 代码:
async function uploadPost() {
const type = 'video/mp4';
let data = new FormData();
const videoUri = imagePreview;
data =
('video',
{
name: 'mobile-video-upload',
type,
videoUri,
});
axios({
method: 'post',
url: 'http://localhost:8080/post/upload',
body: data,
headers: {
'Content-Type': 'multipart/form-data',
},
}).catch(function(error) {
console.log(error);
});
Spring内部的函数:
@CrossOrigin
@ResponseBody
@RequestMapping(value = "/post/upload", method = RequestMethod.POST, consumes = "multipart/form-data")
public String create(@RequestParam("file") MultipartFile file) {
提前致谢。
最佳答案
假设 Spring 代码正确,请尝试使用 Axios 进行细微更改。
const axios = require('axios')
const qs = require('querystring')
....... other code .....
const config = {
headers: {
'Content-Type': 'multipart/form-data'
}
}
axios.post(url, qs.stringify(requestBody), config)
.then((result) => {
// Do somthing
})
.catch((err) => {
// Do somthing
})
关于javascript - Axios POST 请求 : Content-type is set, 但在 spring 内它是空的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59576421/