File Download error:SyntaxError: Unexpected token M in JSON at position 0 Angular 11 HttpPost

File Download error:SyntaxError: Unexpected token M in JSON at position 0 Angular 11 HttpPost

我在 angular 11 中尝试下载文件时遇到上述错误。 状态代码为 200 OK。Asp.Net Web Api 用于 post 文件。

下面是代码。

this.http.post<Blob>(url,{responseType: 'blob'}).subscribe({
    next: versionfiledata=>{
      return  versionfiledata;
    },
    error: error => {
      
      console.error('There was an error!', error);
    }
    
      } )

错误:语法错误:JSON 中位置 0

中的意外标记 M

不确定,但我已尝试将响应类型用作 blob,但使用 Angular 11

时仍然出现错误
 const headers = { 'responseType': 'blob' as 'json'};
    const body = { title: 'Test' };
    return this.http.post<Blob>(url,body,headers).subscribe({next:response=>{
      console.log(response);
    var blob=new Blob([response],{type:"application/octet-stream"});
      FileSaver.saveAs(blob,file);

    }, error: error => {console.log("download error");}
  });

您需要更改 headers 中的回复。