小编Dis*_*ank的帖子

使用Angular 2和Spring MVC中的其他表单字段上传文件

我正在尝试将文件和其他表单字段内容从我的Angular 2前端上传到Spring后端.但不知怎的,我无法做到.这是我的代码:

app.component.ts

fileChange(e){
    this.fileList = e.target.files;
  }

uploadPdf(){
    if(this.fileList.length>0){
      let file: File = this.fileList[0];
      let formData:FormData = new FormData();

      formData.append('uploadFile', file, file.name);
      formData.append('info',this.model);

      console.log(file.size);

      let headers = new Headers();
      headers.append('Accept', 'application/json');
      let options = new RequestOptions({ headers: headers });
      this.http.post(this.url,formData, options)
        /*.map((res: Response) => res.json())*/
        .catch(error => Observable.throw(error))
        .subscribe(
          data =>{
            this.data = data;
            console.log(this.data);
          }
          ,
          error => console.log(error)
        )
    }
  }
Run Code Online (Sandbox Code Playgroud)

app.cmoponent.html

<h1 class="text-center">
  PDF Viewer and Uploader Example
</h1>
<div class="text-center">
  <form …
Run Code Online (Sandbox Code Playgroud)

file-upload spring-mvc angular2-forms angular

4
推荐指数
1
解决办法
5765
查看次数

标签 统计

angular ×1

angular2-forms ×1

file-upload ×1

spring-mvc ×1