Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 错误:您没有收到多部分文件_Angular_Spring Boot_Rest_Multipart - Fatal编程技术网

Angular 错误:您没有收到多部分文件

Angular 错误:您没有收到多部分文件,angular,spring-boot,rest,multipart,Angular,Spring Boot,Rest,Multipart,我有一个java中的REST服务,它必须接收一个多部分文件,但它给出了一个错误,它说它不是来自angular的多部分文件。我留下代码看看是否有人知道问题出在哪里 角度8: sendFile(data: File): Observable<any>{ const headers = new HttpHeaders().set('Content-Type', 'text/html; charset=utf-8'); return this.http.post('http://

我有一个java中的REST服务,它必须接收一个多部分文件,但它给出了一个错误,它说它不是来自angular的多部分文件。我留下代码看看是否有人知道问题出在哪里

角度8:

sendFile(data: File): Observable<any>{
   const headers = new HttpHeaders().set('Content-Type', 'text/html; charset=utf-8');
   return this.http.post('http://localhost:8080/v1/on/file', data,{headers,responseType: 'text'})
    .pipe(
      tap(_ => this.log('send file')),
      catchError(this.handleError('not send file', []))
    );
  }

您必须将其作为FormData传递,而不需要在请求头中指定内容类型:

例如:

sendFile(data: File): Observable<any>{
   var _formData = new FormData();
   _formData.append('file', data);

   return this.http.post('http://localhost:8080/v1/on/file', _formData, { headers, responseType: 'text'})
    .pipe(
      tap(_ => this.log('send file')),
      catchError(this.handleError('not send file', []))
    );
}
sendFile(数据:文件):可观察{
var_formData=new formData();
_formData.append('file',data);
返回此.http.post('http://localhost:8080/v1/on/file“,_formData,{headers,responseType:'text'})
.烟斗(
点击(=>this.log('send file')),
catchError(this.handleError('不发送文件',[]))
);
}

文件
是内置类型还是您已经创建了自己的类型?当我返回MultipartFile文件时,它会给我以下错误:找不到可接受的表示形式,您知道如何修复此问题吗?@MariaGálvez错误来自Java代码或Angular?
sendFile(data: File): Observable<any>{
   var _formData = new FormData();
   _formData.append('file', data);

   return this.http.post('http://localhost:8080/v1/on/file', _formData, { headers, responseType: 'text'})
    .pipe(
      tap(_ => this.log('send file')),
      catchError(this.handleError('not send file', []))
    );
}