在包含 JSPDF blob 的 formData 中找不到请求参数 (REACT + SPRING BOOT + MONGODB)

Request Parameter Not Found in formData Containing a JSPDF blob (REACT + SPRING BOOT + MONGODB)

提问人:Miguel Celos 提问时间:11/10/2023 更新时间:11/10/2023 访问量:22

问:

我正在尝试将带有 pdf blob 的 post 请求从 react 发送到带有 mongodb 的 springboot api。我从另一个组件发布 PNG 文件时已成功完成此操作,但无法使用 PDF 重新创建。我之前尝试过仅发送formData中的“文件”字段,其余详细信息@PathVariables具有相同的结果;“必需部件'文件'不存在。”

控制器

@PostMapping(value="/request/approve", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE, "application/json"})
public ResponseEntity <Request> updateRequest(@RequestParam("response") String response, @RequestParam("employeeEmail") String employeeEmail,
@RequestParam("month") String month, @RequestParam("year") String year, @RequestParam("file") MultipartFile file) throws IOException{
    return new ResponseEntity<Request>(requestService.saveFile(response, employeeEmail, month, year, file), HttpStatus.OK);
}

服务

public Request saveFile(String response, String employeeEmail, String month, String year, MultipartFile file ) throws IOException{
    
    String id = employeeEmail + month +  year;
    System.out.println(id);
    Binary pdf =  new Binary(BsonBinarySubType.BINARY, file.getBytes()); 
    return requestRepository.save(new Request(id, response, month, year, employeeEmail, pdf));
    
}

PDF 生成器和发布请求

const FormData = require('form-data')
const fd = new FormData()

const approveRequest = async() =>{

const input = pdfRef.current;
response = "approved";
html2canvas(input).then((canvas) => {
  const pdf = new jsPDF('p', 'mm', 'a4', true);
  //added pdf specs here, will add in comments for brevity
  setFile(pdf.output('blob'));
  console.log(file)
  

  fd.append('file', file);
  fd.append('response', response);
  fd.append('employeeEmail', employeeEmail);
  fd.append('month', month);
  fd.append('year', year)

  try{
    const res = api.post('/request/approve', fd);
    console.log(res)
  }catch(err){
    console.error(err)
  }
}
reactjs spring-boot multipartform-data jspdf 表单数据

评论


答: 暂无答案