title: Sending file in angular2
body: <p>I need to send file via POST in my web application. I have a server side in java and a client side in angular 2. I need to client send file to the server. Server's code:</p>
  
  <pre><code>@RequestMapping(method = RequestMethod.POST, value = "/run/file")
  @ResponseBody
  private void runImportRecordsJob(@RequestParam("file") MultipartFile file){
      // Some code
  }
  </code></pre>
  
  <p>Client's code:</p>
  
  <p>Component:</p>
  
  <pre><code>export class ImportRecordsJobComponent implements OnInit {
  
    file: File;
  
  
    constructor(private jobsService: JobsService) { }
  
  
    chooseFile(event: any){
      this.file = event.srcElement.files[0];
  
      console.log(this.file);
    }
  
    selectFormat(event: any){
      if (event.length &gt; 0)
        this.format = event[0].key;
      else
        this.format = null;
  
    }
  
    runImportRecordsJob(){
      if (confirm("Are you sure you want to run this job?")){
        this.jobsService.runImportRecordsJob({file: this.file});
      }
    }
  
    ngOnInit() {
    }
  
  }
  </code></pre>
  
  <p>Service:</p>
  
  <pre><code>@Injectable()
  export class JobsService {
  
    constructor(private http: Http) { }
  
    runImportRecordsJob(importRecords: any){
      var headers = new Headers({"Content-Type": 'application/json; multipart/form-data;'});
      let options = new RequestOptions({ headers: headers });
  
      let formData = new FormData();
      formData.append("file", importRecords.file, importRecords.file.name);
  
      this.http.post(SERVER + "/batches/run/file", formData, options).subscribe();
    }
  
  }
  </code></pre>
  
  <p>But I'm getting an error: <code>nested exception is org.springframework.web.multipart.MultipartException: The current request is not a multipart request</code></p>
  
  <p>and <code>formData</code> is always empty. Can anyone suggest me a way how to send file without using ng2-uploader and stuff like that. Thanks.</p>