1

I start by presenting my client side the service

    addImage (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], file

s[i].name);
                }

                xhr.onreadystatechange = () => {
                    if (xhr.readyState === 4) {
                        if (xhr.status === 200) {
                            observer.next(JSON.parse(xhr.response));
                            observer.complete();
                        } else {
                            observer.error(xhr.response);
                        }
                    }
                };

                xhr.upload.onprogress = (event) => {
                    this.progress = Math.round(event.loaded / event.total * 100);

                    this.progressObserver.next(this.progress);
                };

                xhr.open('POST', url, true);
                xhr.send(formData);
            });

    }

then this is my html code

<input type="file" (change)="uploadImage($event)"/>

where I call this method from my component

uploadImage(event) {
        var files = event.srcElement.files;
        console.log(files);
        this._serviceSection.addImage('http://localhost:8080/template/img', [], files).subscribe(() => {
            console.log('sent');
        });
    }

and in my service side

this is a method from my controller

@RequestMapping(value = "/img", method = RequestMethod.POST)
    public void getFileContents(@RequestParam MultipartFile file) {
        System.out.println("++++++++++++++++++++++++++++++++++++++++ " +file.getOriginalFilename());
    }

in a first time a try just to show a fileName but I get this error

enter image description here

1 Answer 1

1

It seems that your progressObserver isn't set. The reason for this could be that you didn't subscribe to its associated observable. Don't forget that observables are lazy and if you don't subscribe to them, their initialization callback isn't called.

To prevent from having the error, you could check if it's null or not:

xhr.upload.onprogress = (event) => {
  if (this.progressObserver) {
    this.progress = Math.round(event.loaded / event.total * 100);

    this.progressObserver.next(this.progress);
  }
};

Otherwise, you can notice that from RC2, Angular2 accepts FormData objects as parameters of HTTP methods...

Sign up to request clarification or add additional context in comments.

1 Comment

how can we get the onprogress callback using the Http service method while we are uploading and using a FormData as a body ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.