我想發送完整曲目以查看和顯示,如 http://localhost:8080/fileaName。然后我推送這個鏈接并將檔案保存在我的磁盤上。
String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
.path("src/main/resources/temp/")
.path(fileName)
.toUriString();
model.addAttribute("fileDownloadUri", fileDownloadUri);
現在我看不到頁面上的鏈接
<span th:href="${fileDownloadUri}"></span>
uj5u.com熱心網友回復:
不確定您的控制器的端點是什么樣的。但我會使用下面的控制器和視圖來顯示檔案的直接下載鏈接。
控制器:
@GetMapping("/downloadFile/{fileId}")
public ResponseEntity<Resource> downloadFile(@PathVariable String fileId) {
// Load file from your source
AppUserDocument appUserDocument = someDocumentStorageService.getFile(fileId);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(appUserDocument.getFileType()))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" appUserDocument.getFileName() "\"")
.body(new ByteArrayResource(appUserDocument.getData()));
}
然后在 thymeleaf 視圖中,我將提供如下所示的下載鏈接:
<td> <a class="btn btn-sm btn-primary" th:href="@{/downloadFile/{id}(id=${myfile.id})}">Download</a></td>
你可以在上面參考我的 github repo:
https://github.com/ajkr195/springbootfileupldnldRESTDB
或者提供對此負責的完整控制器方法。以便 SO 人員可以幫助您。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/375999.html
上一篇:Reactor中的過濾和默認值
