I'v used this method to copy a file to a folder in my project(first method), and I have edited it so the location is stored on my 'Location' in class Submissions (see below).
Now I want to be able to, after clicking on an image in my view, download that file. How can I do that ?
class Submissions {
Date dateSub
String Location
String fileName
}
I have done something similar to following:
Assuming your download page has the relevant Submissions instance...
<g:link action="downloadFile" id="${aSubmission.id}">
<img>...etc...</img>
</g:link>
Then in controller (is your "location" the path to the file?):
def downloadFile = {
def sub = Submissions.get(params.id)
def file = new File("${sub.location}/${sub.fileName")
if (file.exists())
{
response.setContentType("application/octet-stream") // or or image/JPEG or text/xml or whatever type the file is
response.setHeader("Content-disposition", "attachment;filename=\"${file.name}\"")
response.outputStream << file.bytes
}
else render "Error!" // appropriate error handling
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With