简体   繁体   English

Angular Spring 启动(文件+表格数据)上传

[英]Angular Spring Boot (File+FormData) Upload

I want to save to file+form data with angular.我想用 angular 保存到文件+表单数据。 form image I use spring boot as a service.形成图像我使用 spring 作为服务启动。 The code below works when I save the file alone.But I couldn't find how to save (file+title+description).How can I save all at the same time?下面的代码在我单独保存文件时有效。但我找不到如何保存(文件+标题+描述)。如何同时保存所有文件?

*HTML *HTML

<form [formGroup]="informationForm" enctype="multipart/form-data">
            <div class="row">

                <div class="col-sm-3">
                    <div class="form-group">
                        <label for="inputLastName" class="label">Title</label>
                        <input type="text" nbInput fullWidth fieldSize="small" formControlName="title">
                    </div>
                </div>
                
                <div class="col-sm-3">
                    <div class="form-group">
                        <label for="inputLastName" class="label">Description</label>
                        <input type="text" nbInput fullWidth fieldSize="small" formControlName="description">
                    </div>
                </div>
            
                <div class="col-sm-3">
                    <div class="form-group">
                        <label for="inputLastName" class="label">Attachment</label>
                        <input type="file" nbInput fullWidth fieldSize="small" (change)="onFileSelected($event)"
                            formControlName="file">
                    </div>
                </div>

                <div class="col-sm-6">
                    <div class="form-group">
                        <button type="submit" nbButton size="small" (click)="onSubmit()"
                            status="primary">Save</button>
                    </div>
                </div>
            </div>
    </form>

*Component.ts *组件.ts

onFileSelected(event) {
        if (event.target.files.length > 0) {
        const file = event.target.files[0];
        this.selectedFile = file;
        }
    }

  onSubmit() {

    const formData: FormData = new FormData();
    formData.append('file', this.selectedFile);

    this.service.save(formData).subscribe(res => {
      if (res === 'OK') {
        this.alertify.makeToastErrror();
      }
      else {
        this.alertify.makeToastErrror();
      }
    });
   
  }

*Service.ts *服务.ts

save(formData: FormData): Observable<any> {
    return this.apiService.post(apiHost + '/saveFile', formData);
  }

Spring Boot Spring 开机

 @PostMapping(value = "/saveFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseEntity<HttpStatus> save(@RequestParam("file") MultipartFile file) throws IOException {
    .........

}

Do you try it like that?你会这样尝试吗?

const formData: FormData = new FormData();
formData.append('file', this.selectedFile);
formData.append('title', this.title);
formData.append('description', this.description);
user: User = new User();

onSubmit() {
    this.user.name = this.egitimBilgileriForm.get("user").value;
    this.user.title = this.egitimBilgileriForm.get("title").value;

    const formData: FormData = new FormData();
    formData.append('file1', this.selectedFile1);
    formData.append('file2', this.selectedFile2);
    formData.append('user', new Blob([JSON
      .stringify(this.user)], {
      type: 'application/json'
    }));
    
    this.save(formData);
}

 saveManuel(formData: FormData) {
    this.service.createUser(formData).subscribe(res => {
      .
      .
      .
    });
  }
  
  createUser(formData: FormData): Observable<any> {
    return this.apiService.post(apiHost + '/saveUser', formData).pipe(map((data: any) => {
      return data;
    }));
  }


export class User{

user:string,
title:string,
.
.
.
}




 @PostMapping(value = "/saveUser", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseEntity<HttpStatus> createUser(@RequestParam(name = "file1") @Size(min = 1) MultipartFile file1,
            @RequestParam(name = "file2") @Size(min = 1) MultipartFile file2, @RequestPart(name = "user") String user) throws JsonProcessingException, IOException {

        

        User user = new ObjectMapper().readValue(user, User.class);
       

        List<MultipartFile> multipartFileList = new ArrayList<>();
        multipartFileList.add(file1);
        multipartFileList.add(file2);

        List<File> fileList = new ArrayList<>();

        for (MultipartFile multipartFile : multipartFileList) {
            fileList.add(new FileUploader(cvService).uploadCvFile(user, multipartFile));
        }

        cvService.saveUser(user, fileList);

        return ResponseEntity.ok(HttpStatus.OK);

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM