title: Save data in DB and upload file using the same API
body: <p>I want to save data in DB and upload a file using the same api, I use Angular and Srping :</p>
  <p>The problem is I have 2 functions in Angular and 2 functions in Spring, I want to combine them in 1 function Angular and 1 function in Spring. ??</p>
  <p>My object is :</p>
  <pre><code>export class Document {
  
    id: number;
    title: string;
    file: string;       // file name
    directory: string;
    commandId: number;
    createdAt: string;
    updatedAt: string;
    deletedAt: string;
  }
  </code></pre>
  <p>to save data I use :
  Angular :</p>
  <pre><code>const headers = new HttpHeaders({'Access-Control-Allow-Origin': '*'});
  return this.httpClient.post(AppSetting.BASE_URL + AppSetting.DOCUMENTS, document, {headers});
  </code></pre>
  <p>Spring :</p>
  <pre><code>@PostMapping(&quot;/&quot;)
  Document saveDocument(@RequestBody Document document) {
      return docummentService.save(document);
  }
  </code></pre>
  <p>And to Upload the file I use :
  Angular :</p>
  <pre><code>const formData = new FormData();
  formData.append('mage', selectedFile, selectedFile.name);
  
  const headers = new HttpHeaders({'Access-Control-Allow-Origin': '*', 'Content-Type': 'multipart/form-data'});
  this.httpClient.post(AppSetting.BASE_URL + AppSetting.DOCUMENTS, formData,
          {headers, reportProgress: true, observe: 'events'});
  </code></pre>
  <p>Spring :</p>
  <pre><code>@PostMapping(&quot;/upload&quot;)
  public ResponseEntity&lt;ResponseMessage&gt; uploadFile(@RequestParam(&quot;file&quot;) MultipartFile file) {
      String message = &quot;&quot;;
      try {
          docummentService.uploadFile(file);
          message = &quot;Uploaded the file successfully: &quot; + file.getOriginalFilename();
          return ResponseEntity.status(HttpStatus.OK).body(new ResponseMessage(message));
      } catch (Exception e) {
          message = &quot;Could not upload the file: &quot; + file.getOriginalFilename() + &quot;!&quot;;
          return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new ResponseMessage(message));
      }
  }
  </code></pre>