简体   繁体   English

出现“不存在所需的请求部分'文件'”错误

[英]Getting “Required request part 'file' is not present” error

When I try to send a PUT request to Java Spring from the Angular frontend I receive a Required request part 'file' is not present error. 当我尝试从Angular前端向Java Spring发送PUT请求时,收到“ Required request part 'file' is not present错误。 I put the name as 'file' in FormData but I think it somehow doesn't see it like that. 我在FormData中将名称命名为“文件”,但我认为它似乎不像那样。

When I use the same PUT request in Postman it does work. 当我在邮递员中使用相同的PUT请求时,它确实起作用。

This is my service in which the other methods do work: 这是我的服务,其他方法也可以使用:

@Injectable()
export class ExamService {
  examens: Examen[] = [];

  constructor(private http: HttpClient) {}

  getExams(): Observable<Examen[]> {
    return this.http.get<Examen[]>(APIURL + '/all');
  }

  getExamById(id): Observable<Examen> {
    return this.http.get<Examen>(APIURL + '/asobject/' + id);
  }

  updateExamById(id, exam: Examen) {
    const fd = new FormData();
    console.log(exam.file);
    fd.append('name', exam.naam);
    fd.append('file', exam.file);
    return this.http.put<Examen>(APIURL + '/update/' + id, fd);
  }
}

This is the payload that I am sending so it seems it does send it the right way: 这是我要发送的有效负载,因此似乎可以正确发送:

------WebKitFormBoundarynGgRxEgh6IdsxOiR
Content-Disposition: form-data; name="name"

Exam1
------WebKitFormBoundarynGgRxEgh6IdsxOiR
Content-Disposition: form-data; name="file"

4.868,68
------WebKitFormBoundarynGgRxEgh6IdsxOiR--

My controller in Spring which shouldn't be wrong since Postman does work: 我的Spring控制器应该没错,因为Postman确实可以工作:

@PutMapping(value = "/update/{id}", consumes = "multipart/form-data")
public ResponseEntity UpdateExam(@RequestParam("file") MultipartFile file, @RequestParam("name") String name, @PathVariable("id") int id)
{
    Exam newExam = new Exam();
    newExam.setId(id);
    newExam.setSkelet(file.getOriginalFilename());
    newExam.setNaam(name);
    newExam.setCreatedAt(LocalDateTime.now());
    Exam exam2 = ExamFactory.update(newExam);
    examRepository.save(exam2);
    storageService.store(file);
    return created(URI.create("/skelet/exam/update/" + newExam.getId())).build();
}

You append the file with wrong way... Please use this way: 您以错误的方式附加文件...请使用以下方式:

this.formData.append('file', file, file.name);

name 名称

The name of the field whose data is contained in value. 数据包含在值中的字段的名称。

value

The field's value. 字段的值。 This can be a USVString or Blob (including subclasses such as File). 这可以是USVString或Blob(包括子类,例如File)。

filename Optional 文件名可选

The filename reported to the server (a USVString), when a Blob or File is passed as the second parameter. 当将Blob或File作为第二个参数传递时,报告给服务器的文件名(USVString)。 The default filename for Blob objects is "blob". Blob对象的默认文件名是“ blob”。 The default filename for File objects is the file's filename. File对象的默认文件名是文件的文件名。

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

相关问题 Spring Thymeleaf所需的请求部分“文件”不存在 - Spring Thymeleaf Required request part 'file' is not present Tomcat:所需的请求部分“文件”不存在 - Tomcat : Required request part 'file' is not present Spring Boot Required 请求部分“文件”不存在 - Spring Boot Required request part 'file' is not present Spring 文件上传 - “所需的请求部分不存在” - Spring File Upload - 'Required request part is not present' MissingServletRequestPartException:所需的请求部分“文件”不存在 Springboot - MissingServletRequestPartException: Required request part 'file' is not present Springboot 上传图片文件时如何解决“所需的请求部分&#39;文件&#39;不存在”错误 - How to fix “Required request part 'file' is not present” error when uploading image file 上传文件springboot所需的请求部分“文件”不存在 - upload file springboot Required request part 'file' is not present 所需的要求部分“照片”不存在 - Required request part 'photo' is not present 此请求部分“文件”不存在 - This request part 'file' is not present AngularJS JSON Spring MVC 应用程序中的文件上传 400 Bad Request Required 请求部分不存在 - File Upload in AngularJS JSON Spring MVC application 400 Bad Request Required request part is not present
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM