Slater Slater - 4 months ago 1417x
AngularJS Question

File Upload with Angular2 to Rest API

Actually I'm working on a Spring API REST with an interface coded on angular 2.

My problem : CAN'T upload file with angular 2.

My Webresources in java is that :

@RequestMapping(method = RequestMethod.POST, value = "/upload")
public String handleFileUpload(@RequestParam MultipartFile file) {

And it is perfectly working when i call it throught URL request with Auth header etc ....
( with Advanced Rest Client extension for chrome )

proof : (everything work fine in that case )

I added the

<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

In my Srping config file and the Pom dependency


BUT when i try to do the same thing with a webform :

<input type="file" #files (change)="change(files)"/>

With the (change) method :

change(file) {
let formData = new FormData();
formData.append("file", file);
let headers = new Headers({
'Authorization': 'Bearer ' + this.token,
'Content-Type': 'multipart/form-data'
});, formData, {headers}).map(res => res.json()).subscribe((data) => console.log(data));
{method: 'post', body: formData},
{headers: this.headers}

My web service return me an error 500, with that in tomcat logs :

I tried the 'Content-Type': undefined method too but without success ( the webservice return me a 415 error in that case.

Can someone help me to figure out what's the problem is ?

problem solved, i'll update that question later with my code :) but, have a look on the plunker it's working perfectly well ( thanks Thierry Templier ! ) ! :)


In fact, at the moment, you can only provide string input for post, put and patch methods of the Angular2 HTTP support.

To support that, you need to leverage the XHR object directly, as described below:

import {Injectable} from 'angular2/core';
import {Observable} from 'rxjs/Rx';

export class UploadService {
  constructor () {
    this.progress$ = Observable.create(observer => {
      this.progressObserver = observer

  private makeFileRequest (url: string, params: string[], files: File[]): Observable {
    return Observable.create(observer => {
      let formData: FormData = new FormData(),
        xhr: XMLHttpRequest = new XMLHttpRequest();

      for (let i = 0; i < files.length; i++) {
        formData.append("uploads[]", files[i], files[i].name);

      xhr.onreadystatechange = () => {
        if (xhr.readyState === 4) {
          if (xhr.status === 200) {
          } else {

      xhr.upload.onprogress = (event) => {
        this.progress = Math.round(event.loaded / * 100);;
      };'POST', url, true);

See this plunkr for more details:

There is a an issue and a pending PR regarding this in the Angular repo: