简体   繁体   English

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

[英]Java : Cannot send a form with Angular 5 that contain an input of type file to a REST backend (Spring Boot)

i am working in a project in school, backend is a REST based on Spring Boot, 我在学校的一个项目中工作,后端是基于Spring Boot的REST,
Frontend is an Angular 5 application. 前端是Angular 5应用程序。 i have red a lot of tutorials but i cannot 我有很多教程,但我不能
find the right answer for my question: 找到适合我的问题的正确答案:
-How can i post a form that contain an input of type file and others input of type text -如何发布包含输入类型的文件和其他输入类型的文本的表单
-i want to send a form that contain a picture to backend, after that i want to take the file, rename it with unique name and upload it to a folder and put the URL in the DataBase -我要发送一个包含图片的表单到后端,之后我要获取文件,使用唯一的名称对其进行重命名,然后将其上传到文件夹,然后将URL放入数据库中
-i Have this Error in Backend : -i在后端有此错误:
Unexpected character ('-' (code 45)) in numeric value: expected digit (0-9) to follow minus sign, for valid numeric value 数值中的意外字符(“-”(代码45)):有效数字值后跟预期数字(0-9)

Here is the Entity: 这是实体:

@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-------------

}

Here is the RestController Class : 这是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);
  }
}

Here is the front end App: 这是前端应用程序:

<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>

Here is the TypeScript Controller: 这是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");

    }, ()=>{

    });

    }

Here is the service : 这是服务:

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);
 }
 }

When uploading the file to the backend you can use Multipart file. 将文件上传到后端时,您可以使用Multipart文件。 You could also Base64 encode the file in the frontend and send it as a JSON string, but that causes a lot more bytes being sent over the wire. 您还可以在前端对Base64进行编码,然后将其作为JSON字符串进行发送,但这会导致通过电线发送更多的字节。

In the multipart solution your controller should expect a multipart file like so. 在多部分解决方案中,您的控制器应期望这样的多部分文件。

Controller 控制者

@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";
  }

}

Your entity can't just have a File class as field. 您的实体不能只是将File类作为字段。 You need to have a byte[] ; 您需要有一个byte[] ; you will ultimately be storing raw bytes in your database - I think other datatypes are also allowed. 您最终将在数据库中存储原始字节-我认为也可以使用其他数据类型。

Entity 实体

@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-------------

}

For the Angular part I suppose this is a perfect example of what you need to upload files: https://stackoverflow.com/a/40216616/5473627 对于Angular部分,我想这是您需要上传文件的完美示例: https : //stackoverflow.com/a/40216616/5473627

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

相关问题 无法将基于Angular 5包含Angular 5的对象(外键)的POST请求发送到基于Spring Boot的Rest Backend - Cannot send a POST request that contain an object (foreign key) with Angular 5 to Rest Backend based on Spring Boot Java:无法使用Angular 5将POST请求发送到后端 - Java : Cannot send POST request to backend with Angular 5 在 spring boot 2.0.5 中将具有不同数据类型的图像从 angular 7 发送到 Rest API - Send image with difference data type from angular 7 to Rest API in spring boot 2.0.5 Spring Boot Rest API - 输入文件 + 端点 - Spring Boot Rest API - input file + endpoints Thymeleaf:无法处理Spring Boot的输入形式 - Thymeleaf: cannot handling input form Spring Boot Angular 与 REST 在 Spring 启动 - Angular with REST in Spring BOOT 从后端(Java / Jersey / REST)将数据发送到前端(Angular JS) - Send data to Frontend(Angular JS) from backend (Java/Jersey/REST) Angular 前端和 Java Spring 引导后端中的 CORS 策略阻止了请求? - Request blocked by CORS policy in Angular frontent and Java Spring Boot backend? Spring 引导后端与 angular 前端 - Spring boot backend with angular frontend JavaScript:确保表单值唯一,Java Spring Rest作为后端 - JavaScript: Making sure form values are unique, Java Spring Rest as backend
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM