我已经尝试修复这个问题两天了,但是没有任何效果。我只是尝试使用HttpClient从输入元素发送一个图像。我的打字脚本是:
public onImageSubmit(input) {
this.selectedFile = input.files[0];
var uploadImageData = new FormData();
uploadImageData.append('imageFile', this.selectedFile);
const httpOptions = {
headers: new HttpHeaders({
'Authorization': 'eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2R0byI6IntcInVzZXJJZFwiOjEsXCJ1c2VybmFtZVwiOlwiZ2VvcmdlXCJ9In0.pBWSj-SWVj9FuPd3r2S5MJo7AZ62mwm-MjBKj409vUYi1YuCieh1FWm0XGISHkK2cF51Tu5fzeKEwg3rr8Ya1w'
'Content-Type': 'multipart/form-data' // I have also tried without this
})
}
this.http.post<Image>('http://localhost:9090/users/1/images', uploadImageData, httpOptions).subscribe(response => {
console.log(response);
});
}
我有一个Spring Web服务器在后台运行。我在服务器上收到错误“Current request is not a multipart request”。检查Firefox中的请求头显示Content-Type
是application/json
。我试过在头中显式指定内容类型,也试过忽略它并希望浏览器会这样做。这两种方法都不起作用。有人知道我做错了什么吗?
1条答案
按热度按时间mrzz3bfm1#
请尝试使用
form-data-encoder
https://github.com/octet-stream/form-data-encoder或者,您是否检查了tsconfig.json compilerOptions.target?我在目标为
es2020
时遇到了类似的问题,但当目标为es2015
时却没有出现这种问题。请理解,这不是一个理想的解决方案,因为某些依赖项可能需要es2020
。