繁体   English   中英

Primeng p-fileupload 组件可以开箱即用地处理返回类型吗?

[英]Can primeng p-fileupload component handle return type out-of-the-box?

我正在使用primeng p-fileupload组件上传单个文件。 后端 API 是Spring Boot上传文件到hdfs并返回路径的引导。 文件上传正常,但未onUpload() angular function。 我知道这是由于返回的路径变量,因为它使用void返回类型。 如何使用默认的p-fileupload组件获取路径(无需编写自定义上传功能)?

<p-fileUpload name="file" url="http://<MY-IP>:8080/upload" accept=".csv" maxFileSize="1000000" (onUpload)="onUpload($event)"></p-fileUpload>
onUpload(event) {
    for (let file of event.files) {
      console.log(file);
      this.uploadedFiles.push(file);
      console.log(this.uploadedFiles);
    }
 }

想通了。

我的Spring Boot API 返回一个String而不是 JSON。 正确的代码返回带有响应 object 的ResponseEntity ,该响应具有一个名为path的变量(这在引擎盖下被序列化为 JSON)。

    @PostMapping("/upload")
    public ResponseEntity upload(@RequestParam("file") MultipartFile file) {
        final String path = fileUploadService.uploadFile(file, fileStorageProperties.getUploadDir());
        return ResponseEntity.status(HttpStatus.CREATED).body(new UploadFileResponse(path));
    }

Angular中,可以使用 event.originalEvent.body.path 返回event.originalEvent.body.path

onUpload(event) {
    for (let file of event.files) {
        let path: string = event.originalEvent.body.path;
        console.log(path);
    }
}

暂无
暂无

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

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