Java:无法将包含类型文件输入的Angular 5表单发送到REST后端(Spring Boot)

时间:2018-10-15 13:39:14

标签: java spring angular rest spring-boot

我正在学校的一个项目中工作,后端是基于Spring Boot的REST,
前端是Angular 5应用程序。我有很多教程,但我不能
找到适合我的问题的正确答案:
-如何发布包含文件类型输入和其他文本类型输入的表单
-i要发送一个包含图片的表单到后端,之后我要获取该文件,使用唯一的名称对其进行重命名,然后将其上传到一个文件夹,然后将URL放入数据库中
-i在后端出现此错误:
数值中的意外字符(“-”(代码45)):有效数字值后,预期数字(0-9)跟随负号

这是实体:

@Entity
public class Prestataires implements Serializable {

  @Id @GeneratedValue
  private Long id;
  private String nom;
  private String email;
  private String tele;
  private String fax;
  private String rib;
  private String adresse;
  private String taches;
  private String photo;
  private File file;

//-------------------Constructors--------------------

//-------------------Getters and Setters-------------

}

这是RestController类:

package smart.syndic.web;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import smart.syndic.dao.PrestatairesRepository;
import smart.syndic.entities.Prestataires;

@RestController
@CrossOrigin("*")
public class PrestatairesRestController {
  @Autowired
  private PrestatairesRepository repository;

  @RequestMapping(value="/prestataires", 
          method=RequestMethod.POST)
  public Prestataires addPrestataires(
            @RequestBody Prestataires v) {
    /*
    Here will be the code to process the file coming from front End and
    uploading it to folder then put the URL to DataBase
    */
    return repository.save(v);
  }
}

这是前端应用程序:

<form class="form-horizontal form-label-left" #f1="ngForm">
            <div id="containerAjouterPrestataires">

            </div>
            <div class="form-group">
              <label class="control-label col-md-3 col-sm-3 col-xs-12">Raison Social/Nom<span class="required">*</span>
              </label>
              <div class="col-md-6 col-sm-6 col-xs-12">
                <input [(ngModel)]="nom" name="nom" type="text" required class="form-control col-md-7 col-xs-12">
              </div>
            </div>

            <div class="form-group">
              <label class="control-label col-md-3 col-sm-3 col-xs-12">Email<span class="required">*</span>
              </label>
              <div class="col-md-6 col-sm-6 col-xs-12">
                <input [(ngModel)]="email" name="email" type="email" required class="form-control col-md-7 col-xs-12">
              </div>
            </div>

            <div class="form-group">
              <label class="control-label col-md-3 col-sm-3 col-xs-12">Téléphone<span class="required">*</span></label>
              <div class="col-md-6 col-sm-6 col-xs-12">
                <input [(ngModel)]="tele" name="tele" class="form-control col-md-7 col-xs-12" type="text" required>
              </div>
            </div>

            <div class="form-group">
              <label class="control-label col-md-3 col-sm-3 col-xs-12">Fax<span class="required">*</span></label>
              <div class="col-md-6 col-sm-6 col-xs-12">
                <input [(ngModel)]="fax" name="fax" class="form-control col-md-7 col-xs-12" type="text" required>
              </div>
            </div>

            <div class="form-group">
              <label class="control-label col-md-3 col-sm-3 col-xs-12">RIB<span class="required">*</span></label>
              <div class="col-md-6 col-sm-6 col-xs-12">
                <input [(ngModel)]="rib" name="rib" class="form-control col-md-7 col-xs-12" type="text" required>
              </div>
            </div>

            <div class="form-group">
              <label class="control-label col-md-3 col-sm-3 col-xs-12">Type<span class="required">*</span></label>
              <div class="col-md-6 col-sm-6 col-xs-12">

                <div class="input-group">
                  <select class="form-control" name="selectTypes" [(ngModel)]="selectTypes">
                    <option selected="selected" *ngFor="let s of tousLesPrestatairesTypes" [value]="s.id" >
                      {{s.designation}}
                    </option>
                  </select>
                  <span class="input-group-btn">
                    <!-- Button trigger modal -->
                    <button type="button" class="btn btn-default" data-toggle="modal" data-target="#myModal">
                      Ajouter Type
                    </button>
                  </span>
                </div>
              </div>
            </div>

            <div class="form-group">
              <label class="control-label col-md-3 col-sm-3 col-xs-12">Adresse<span class="required">*</span>
              </label>
              <div class="col-md-6 col-sm-6 col-xs-12">
                <textarea [(ngModel)]="adresse" name="adresse" class="form-control" rows="3" placeholder="Adresse"></textarea>
              </div>
            </div>

            <div class="form-group">
              <label class="control-label col-md-3 col-sm-3 col-xs-12">Tâches<span class="required">*</span>
              </label>
              <div class="col-md-6 col-sm-6 col-xs-12">
                <textarea [(ngModel)]="taches" name="taches" class="form-control" rows="3" placeholder="Tâches"></textarea>
              </div>
            </div>

            <div class="form-group">
              <label class="control-label col-md-3 col-sm-3 col-xs-12">Photo/Logo<span class="required">*</span></label>
              <div class="col-md-6 col-sm-6 col-xs-12">
                <input name="photo" class="form-control col-md-7 col-xs-12"
                       type="file" required="required" accept="image/*"
                        (change)="handleFileInput($event)">
              </div>
            </div>

<div class="form-group">
              <div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
                <button class="btn btn-warning" type="reset">Vider</button>
                <button type="button" class="btn btn-success" (click)="ajouterPrestataires()">Ajouter</button>
              </div>
            </div>



          </form>

这是TypeScript控制器:

import { Component, OnInit } from '@angular/core';
import {PrestatairesService} from "../../services/prestataires.service";
import {PrestatairesTypeModel} from "../../modeles/prestatairesType.model";
import {PrestatairesModel} from "../../modeles/prestataires.model";

@Component({
selector: 'app-ajouter-prestataires',
templateUrl: './ajouter-prestataires.component.html',
styleUrls: ['./ajouter-prestataires.component.css']
})
export class AjouterPrestatairesComponent implements OnInit {

nom:any;
email:any;
tele:any;
fax:any;
rib:any;
adresse:any;
taches:any;
photo:any;

selectTypes:any;

typePrestataire:any;

tousLesPrestatairesTypes:any;

modelType:any;

imageURL:string = "../assets/images/MeG.jpg";

fileToUpload:File = null;

modelPrestataires:any;


constructor(private service:PrestatairesService) { }

ngOnInit()
{
   this.getAllTypes();
}

handleFileInput(file:any)
{
  this.fileToUpload = <File>file.target.files[0];

}

ajouterPrestataires()
{

  this.modelPrestataires = new PrestatairesModel();
  this.modelPrestataires.nom = this.nom;
  this.modelPrestataires.email = this.email;
  this.modelPrestataires.tele = this.tele;
  this.modelPrestataires.fax = this.fax;
  this.modelPrestataires.rib = this.rib;
  this.modelPrestataires.adresse = this.adresse;
  this.modelPrestataires.taches = this.taches;
  this.modelPrestataires.file = this.fileToUpload;
  this.modelPrestataires.photo = this.photo;

  this.getOneType(this.selectTypes);
  this.modelPrestataires.prestatairesTypes = this.modelType;

  this.service.uploadFile(this.modelPrestataires)
    .subscribe(data=>{


console.log("Success");

    }, err=>{


console.log("Error");

    }, ()=>{

    });

    }

这是服务:

import {Injectable} from "@angular/core";
import {HttpClient, HttpEvent, HttpHeaders, HttpParams, HttpRequest} from 
"@angular/common/http";

@Injectable()
export class PrestatairesService
{
 host:string = "http://localhost:8080/";
   constructor(private http:HttpClient)
 {

 }
 uploadFile(model:any){

 let formData = new FormData();

  formData.append('fichier', model.file);

  let headers = new HttpHeaders().set('Content-Type','application/json');


  let params = new HttpParams();
  const options = {
  params: params,
  reportProgress: true,
  headers: headers
 };

 const req = new HttpRequest('POST', this.host + "prestataires", formData, 
 options);
 return this.http.request(req);
 }
 }

1 个答案:

答案 0 :(得分:0)

将文件上传到后端时,可以使用Multipart文件。您还可以使用Base64在前端对文件进行编码,然后将其作为JSON字符串发送,但这会导致通过电线发送更多的字节。

在多部分解决方案中,您的控制器应该期望这样的多部分文件。

控制器

@RestController
@CrossOrigin("*")
public class PrestatairesRestController {
  @Autowired
  private PrestatairesRepository repository;

  @RequestMapping(value="/prestataires", method=RequestMethod.POST)
  // you don't have to add @RequestBody for the Prestataires
  public String postFileUpload(Prestataires prestataires, @RequestParam("multipartFile") MultipartFile file) {
    // Make sure that in the frontend the name of the form field for the file is also multipartFile
   // Also make sure that the mime type in the frontend is multipart/form-data

    byte[] rawFile;

    try {
      rawFile = file.getBytes();
    } catch (IOException e) {
      e.printStackTrace();
      return "error?";
    }

    prestataires.setFile(rawFile);

    prestatairesRepository.save(prestataires);

    return "redirect:/ or send response";
  }

}

您的实体不能仅具有File类作为字段。您需要拥有一个byte[];您最终将在数据库中存储原始字节-我认为也可以使用其他数据类型。

实体

@Entity
public class Prestataires implements Serializable {

  @Id @GeneratedValue
  private Long id;
  private String nom;
  private String email;
  private String tele;
  private String fax;
  private String rib;
  private String adresse;
  private String taches;
  private String photo;

  @Lob
  @Column(name = "file", columnDefinition="BLOB")
  private byte[] file;

  //-------------------Constructors--------------------

  //-------------------Getters and Setters-------------

}

对于Angular部分,我想这是您需要上传文件的完美示例:https://stackoverflow.com/a/40216616/5473627