java - Creating a file download link using Spring Boot and Thymeleaf -
this might sound trivial question, after hours of searching yet find answer this. problem, far understand, trying return filesystemresource
controller , thymeleaf expects me supply string
resource using render next page. since returning filesystemresource
, following error:
org.thymeleaf.exceptions.templateinputexception: error resolving template "products/download", template might not exist or might not accessible of configured template resolvers
the controller mapping have used is:
@requestmapping(value="/products/download", method=requestmethod.get) public filesystemresource downloadfile(@param(value="id") long id) { product product = productrepo.findone(id); return new filesystemresource(new file(product.getfileurl())); }
my html link looks this:
<a th:href="${'products/download?id=' + product.id}"><span th:text="${product.name}"></span></a>
i don't want redirected anywhere, need file downloaded once link clicked. right implementation? i'm not sure.
you need change th:href
like:
<a th:href="@{|/products/download?id=${product.id}|}"><span th:text="${product.name}"></span></a>
then need change controller , include @responsebody
annotation:
@requestmapping(value="/products/download", method=requestmethod.get) @responsebody public filesystemresource downloadfile(@param(value="id") long id) { product product = productrepo.findone(id); return new filesystemresource(new file(product.getfileurl())); }