0

Angular 4.0 및 SpringBoot Application을 사용하여 json 파일을 업로드하려고합니다. 나는 체크하고 Stackoverflow에서 다른 솔루션을 시도했지만 정확한 문제가 무엇인지를 설명 할 수는 없다.FileUpload Multipart Springboot Error -> 'File'필수 요청 부분이 없습니다.

다음과 같은 메시지와 함께 400 BAD Request Error 메시지가 나타납니다. 'file'필수 요청 부분이 없습니다.

My RestController는 (테스트 목적으로) 이렇게 보이지만 불행히도 아무 일도 발생하지 않습니다.

@RestController 
@RequestMapping("/api") 
public class UploadRequestResource { 
.... 

@PostMapping("/fileupload") 
@Timed 
public ResponseEntity<Endpoint> FileUpload(@RequestParam("file") MultipartFile file) throws URISyntaxException { 
     if (file.isEmpty()) { 
      System.out.println("File is empty"); } 

     System.out.println("File is not empty"); 

     //some logic 

     return ResponseEntity.ok() .... 
    } 
} 

나는 내 applicatoin 구성 파일에 다음을 추가 한 :

spring: 
    http: 
     multipart: 
      max-file-size: 5MB 
      max-request-size: 20MB 

내 HTML 파일은 다음과 같습니다

각 TS 파일은 다음과 같습니다
<form name="editForm" role="form" novalidate (ngSubmit)="save()" #editForm="ngForm"> 
    ... 
    <input type="radio" [(ngModel)]="form.uploadType" name="uploadType" value="file">&nbsp;<label for="url">File</label><br> 
    <input type="file" name="file" placeholder="Upload file..." (change)="onFileChange($event)" (focus)="onFileFocus()"/> 
      </div> 
     </div> 

:

fileUpload(data): Observable<Endpoint> { 
     let headers = new Headers({ 'Content-Type': 'multipart/form-data' }); 
     let options = new RequestOptions({ headers: headers }); 
     return this.http.post('/api/fileupload', data , options).map((res: Response) => { 
      const jsonResponse = res.json(); 
      return this.convertItemFromServer(jsonResponse); 
     }); 
    } 

D oes 누군가가이 오류를 해결하는 방법을 알고있다? 나는 어떤 도움을 주셔서 너무나 감사 할 것입니다. 감사합니다

+0

POST 요청에 대한 enctype을 multipart/form-data로 업데이트하십시오. – MohamedSanaulla

+0

POST 요청에 대한 multipart/form-data에 enctype을 이미 정의했습니다 ... 내 초기 게시물을 수정했습니다 ... – curlie

+0

'

'태그에'enctype' 속성을 추가 할 수 있습니다. – MohamedSanaulla

답변

0

그래서 내 문제에 대한 해결책을 찾았습니다. 대신 Content-Type : "multipart/form-data"를 사용하여 Formdata를 사용했습니다 (아래 참조).

const formData = new FormData(); 
     formData.append('file', data, data.name); 
     return this.http.post('/api/fileupload', formData).map((res: Response) => { 
      const jsonResponse = res.json(); 
      return this.convertItemFromServer(jsonResponse); 

이제 제대로 작동합니다.