当前请求不是多部分请求

huangapple go评论71阅读模式
英文:

Spring Boot - Angular: Current request is not a multipart request

问题

以下是您要翻译的内容:

"I'm trying to upload a file from Angular front-end to Spring Boot back-end.

HTML:

<button type="button" (click)="fileInput.click()">
    <span>Upload</span>
    <input #fileInput type="file" (change)="onFileChange($event)" style="display: none" />
</button>

<button class="button" (click)="uploadDocumento()">Upload</button>

TS:

onFileChange(event: any) {
    this.file = event.target.files[0];
}

uploadDocumento() {
    if (this file) {
        this.baseService.uploadfile(this.file, this.url).subscribe(resp => {});
    }
}

public uploadfile(file: File, url: string) {
    let formParams = new FormData();
    formParams.append('file', file);
    return this.httpClient.post(url, formParams);
}

Spring Boot:

@PostMapping("/uploadFile")
public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) {
    LOGGER.info(file);
    return null;
}

I get the error: "org.springframework.web.multipart.MultipartException: Current request is not a multipart request."

What am I doing wrong? Thanks in advance"

注意:原始内容中的HTML、TS和Spring Boot代码已被保留,但文本中的HTML和TS代码已翻译成普通文本格式,以方便阅读。

英文:

I'm trying to upload a file from Angular front-end to Spring Boot back-end.

HTML:

&lt;button type=&quot;button&quot; (click)=&quot;fileInput.click()&quot;&gt;
    &lt;span&gt;Upload&lt;/span&gt;
    &lt;input #fileInput type=&quot;file&quot; (change)=&quot;onFileChange($event)&quot; style=&quot;display: none&quot; /&gt;
&lt;/button&gt;

&lt;button class=&quot;button&quot; (click)=&quot;uploadDocumento()&quot;&gt;Upload&lt;/button&gt;

TS:

  onFileChange(event: any) {
        this.file = event.target.files[0];
    }

uploadDocumento() {
        if (this.file) {
            this.baseService.uploadfile(this.file, this.url).subscribe(resp =&gt; {});
        }
    }

  public uploadfile(file: File, url: string) {
        let formParams = new FormData();
        formParams.append(&#39;file&#39;, file);
        return this.httpClient.post(url, formParams);
    }

Spring Boot:

  @PostMapping(&quot;/uploadFile&quot;)
    public ResponseEntity&lt;String&gt; uploadFile(@RequestParam(&quot;file&quot;) MultipartFile file) {
        LOGGER.info(file);
        return null;
    }

I get the error: "org.springframework.web.multipart.MultipartException: Current request is not a multipart request"

What am I doing wrong? Thanks in advance

答案1

得分: 1

尝试在前端再次操作,您需要使用具有以下标头的请求传递下去:

'Content-Type': 'multipart/form-data'

英文:

Try again on frontend, you need transfer request down with header has more:

&#39;Content-Type&#39;: &#39;multipart/form-data&#39;

答案2

得分: 0

你可以尝试通过以下方式在后端替换 API 的标题:

@RequestMapping(value = "/uploadFile", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
英文:

you can try to replace the header of api in backend by
this line.

    @RequestMapping(value = &quot;/uploadFile&quot;, 
method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)

答案3

得分: 0

已解决。我的拦截器会将" 'Content-Type': 'application/json' "添加到任何HTTP请求中。

英文:

Resolved. My interceptor add "'Content-Type': 'application/json'" to any http request.

huangapple
  • 本文由 发表于 2023年2月23日 19:52:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75544469.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定