angularjs HttpClient请求中的内容类型未更改为“multipart/form-data”

oknwwptz  于 2022-10-31  发布在  Angular
关注(0)|答案(1)|浏览(183)

我已经尝试修复这个问题两天了,但是没有任何效果。我只是尝试使用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-Typeapplication/json。我试过在头中显式指定内容类型,也试过忽略它并希望浏览器会这样做。这两种方法都不起作用。有人知道我做错了什么吗?

mrzz3bfm

mrzz3bfm1#

请尝试使用form-data-encoderhttps://github.com/octet-stream/form-data-encoder
或者,您是否检查了tsconfig.json compilerOptions.target?我在目标为es2020时遇到了类似的问题,但当目标为es2015时却没有出现这种问题。请理解,这不是一个理想的解决方案,因为某些依赖项可能需要es2020

相关问题