File Upload with Angular2 to REST API

后端 未结 9 1068
萌比男神i
萌比男神i 2020-11-27 10:14

Actually, I\'m working on a Spring REST API with an interface coded in Angular 2.

My problem is I can\'t upload a file with Angular 2.

My Webresources in jav

9条回答
  •  旧巷少年郎
    2020-11-27 11:10

    This has worked for me:

    
    onChange(event: any) {
        let fileList: FileList = event.target.files;
    if(fileList.length > 0) {
        let file: File = fileList[0];
        let formData:FormData = new FormData();
        formData.append('degree_attachment', file, file.name);
        let headers = new Headers();
        headers.append('Accept', 'application/json');
        let options = new RequestOptions({ headers: headers });
        this.http.post('http://url', formData,options)
            .map(res => res.json())
            .catch(error => Observable.throw(error))
            .subscribe(
                data => console.log('success'),
                error => console.log(error)
            )
    }}
    

提交回复
热议问题