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

Dis*_*ank 4 file-upload spring-mvc angular2-forms angular

我正在尝试将文件和其他表单字段内容从我的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 enctype="multipart/form-data">
    <div class="form-group">
      <label for="name">Name: </label>
      <input type="text" id="name" class="form-control" name="name" [(ngModel)]="model.name">
    </div>
    <div class="form-group">
      <label for="email">Email: </label>
      <input type="email" id="email" class="form-control" name="email" [(ngModel)]="model.email">
    </div>
    <div class="form-group">
      <label for="pdfInput">Select File: </label>
      <input type="file" id="pdfInput" class="form-control" name="pdfInput" (change)="fileChange($event)">
    </div>
    <div class="form-group">
      <button type="button" class="btn btn-success" (click)="uploadPdf()">Upload File!</button><span>   </span>
      <button type="button" class="btn btn-success" (click)="printData()">Print to Console!</button>
    </div>
  </form>
</div>
Run Code Online (Sandbox Code Playgroud)

model.ts

export class Model{

  name: string;
  email: string;

}
Run Code Online (Sandbox Code Playgroud)

现在在后端:

ExampleModel.java

public class ExampleModel {

private String name;
private String email;
//Getters and Setters
Run Code Online (Sandbox Code Playgroud)

MainController.java

@RequestMapping(value = "/file",method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> addUser(@RequestParam("uploadFile") MultipartFile file, @RequestParam("info") ExampleModel model){}
Run Code Online (Sandbox Code Playgroud)

那么,如何info在弹簧控制器中获得标记数据?上面我已经展示了我的错误尝试,获取其他表单字段数据的正确方法是什么?

应该如何定义带注释的Controller方法,还是有其他方式从Angular 2发送数据(文件+表单字段)?

Ste*_*ota 9

您需要使用@RequestPart而不是@RequestParam和设置consumes属性:

@RequestMapping(value = "/file",method = RequestMethod.POST, consumes = "multipart/form-data")
@ResponseBody
public ResponseEntity<String> addUser(@RequestPart("uploadFile") MultipartFile file, @RequestPart("info") ExampleModel model){}
Run Code Online (Sandbox Code Playgroud)

您还需要调整FormData对象:

formData.append('uploadFile', file, file.name);
formData.append('info', new Blob([JSON.stringify(this.model)],
        {
            type: "application/json"
        }));
Run Code Online (Sandbox Code Playgroud)