如何在角度7中将内容类型设置为multipart/form-data

Dus*_*han 5 multipartform-data angular

我知道这类问题在这里和一些论坛上已经问过好几次了。

我想做的是将文件上传到我的服务器。下面是我第一次尝试将标题设置为multipart/form-data. 以下是我的文件上传服务。

uploadNewFile (formData): Observable<any> {
    const headers = new HttpHeaders();
    headers.set('Content-Type', undefined);
    return this.http.post(environment.baseURL+'api/v1/company/someFileUpload' , formData, {headers: headers})
      .pipe(
        catchError(this.formatErrors)
      );
  }
Run Code Online (Sandbox Code Playgroud)

但是当我这样做时,我收到了这个错误。

错误:多部分:未找到边界

但是在看到这个问题( Send multipart/form-data files with Angular using $http )之后,我将标题更改为这个。

const headers = new HttpHeaders();
headers.set('Content-Type', undefined)
Run Code Online (Sandbox Code Playgroud)

但是当我这样改变它时,我收到了这个错误。

在此输入图像描述

但是当我检查该错误的答案时,它说我需要在此处添加一个处理程序(Uncaught TypeError: Cannot read property 'ngOriginalError' of undefined at getOriginalError - 当 httpClient returned string

但我有一个为我服务的处理程序。服务粘贴在下面。

uploadNewFile (formData): Observable<any> {
    const headers = new HttpHeaders();
    headers.set('Content-Type', undefined);
    return this.http.post(environment.baseURL+'someFileUpload' , formData, {headers: headers})
      .pipe(
        catchError(this.formatErrors)
      );
  }
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

小智 6

检查拦截器,因为您可能在拦截器中为应用程序json设置了cintent类型