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