为什么在使用POST上传文件后页面会重新加载?(Angular + Spring Boot)

huangapple 未分类评论58阅读模式
英文:

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

问题

文件上传功能正常,但在从后端接收响应后,应用程序正在重新加载。 <br/>

Angular 代码 <br />
HTML<br />

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

&lt;button class=&quot;mat-raised-button&quot; [disabled]=&quot;this.uploadReport.invalid&quot; (click)=&quot;saveReport()&quot;&gt;上传文档&lt;/button&gt;

TS<br />

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

saveReport(category :string, dataFile : FormData): Observable&lt;ApiResponse&gt; {
    return this.http.post&lt;ApiResponse&gt;(this.ROOT_URL + &#39;/report/&#39; + category  , dataFile, {reportProgress: true, responseType: &#39;json&#39;});
}

Spring Boot<br/>

@PostMapping(&quot;/{category}&quot;)
public ResponseEntity&lt;Response&lt;String&gt;&gt; saveReport(@RequestParam(&quot;file&quot;) MultipartFile multipartFile,
                                                           @PathVariable String category) throws IOException 
    return ResponseEntity.ok(reportService.saveReport(category, multipartFile));
}
英文:

File Uploading is working but the app is reloading after receiving the response from the backend. <br/>
Angular Code <br />
HTML<br />

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

&lt;button class=&quot;mat-raised-button&quot; [disabled]=&quot;this.uploadReport.invalid&quot; (click)=&quot;saveReport()&quot;&gt;Upload Document&lt;/button&gt;

TS<br />

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

saveReport(category :string, dataFile : FormData): Observable&lt;ApiResponse&gt; {
    return this.http.post&lt;ApiResponse&gt;(this.ROOT_URL + &#39;/report/&#39; + category  , dataFile, {reportProgress: true, responseType: &#39;json&#39;});
  }

Spring boot<br />

@PostMapping(&quot;/{category}&quot;)
    public ResponseEntity&lt;Response&lt;String&gt;&gt; saveReport(@RequestParam(&quot;file&quot;) MultipartFile multipartFile,
                                                       @PathVariable String category) throws IOException 
        return ResponseEntity.ok(reportService.saveReport(category, multipartFile));
    }

答案1

得分: 0

我认为应用程序没有因为从后端接收响应而重新加载。您的上传按钮是否是表单中唯一的按钮?如果是这样,尝试阻止表单的提交行为:

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

或者

在那个按钮上添加 type='button'。在表单中,如果一个按钮没有定义类型,它的行为类似于提交按钮。

英文:

I don't think the app is reloading due to receiving the response from the backend. Is your uploading button the only button in a form? if then try to prevent submitting behaviour of the form:

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

or

> Add type='button' to that button. A button without any defined type in a form acts like a submit button

huangapple
  • 本文由 发表于 2020年7月24日 11:49:03
  • 转载请务必保留本文链接:https://java.coder-hub.com/63066508.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定