繁体   English   中英

为什么使用帖子上传文件后页面会重新加载? (角 + Spring 引导)

[英]Why the page is reloading after using a post to upload a file? (Angular + Spring Boot )

文件上传正常,但应用程序在收到后端响应后正在重新加载。
Angular代码
HTML

<input hidden="true" accept="text/pdf, .pdf" type="file" #pdfInput id="report"
                       (change)="validateFile($event)">
<mat-card class="reportName">
   <input readonly matInput name="fileHidden" formControlName="pdfFile">
</mat-card>

<button class="mat-raised-button" [disabled]="this.uploadReport.invalid" (click)="saveReport()">Upload Document</button>

TS

saveReport() {
    console.log(this.uploadReport.value.category);
    this.reportService.saveReport(this.uploadReport.value.category, this.uploadFile).subscribe((val) => {
      console.log(val);
    })
  }

saveReport(category :string, dataFile : FormData): Observable<ApiResponse> {
    return this.http.post<ApiResponse>(this.ROOT_URL + '/report/' + category  , dataFile, {reportProgress: true, responseType: 'json'});
  }

Spring开机

@PostMapping("/{category}")
    public ResponseEntity<Response<String>> saveReport(@RequestParam("file") MultipartFile multipartFile,
                                                       @PathVariable String category) throws IOException 
        return ResponseEntity.ok(reportService.saveReport(category, multipartFile));
    }

我认为应用程序不会因为收到来自后端的响应而重新加载。 您的上传按钮是表单中唯一的按钮吗? 如果然后尝试阻止表单的提交行为:

<button (click)="saveReport(); $event.preventDefault()">

或者

将 type='button' 添加到该按钮。 表单中没有任何定义类型的按钮就像提交按钮

暂无
暂无

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

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