Search code examples
javagrailsgroovy

Grails 3 Loading Static Files


I have a few PDF files in the assets folder of a Grails 3 application. I want to load them when the user clicks some button. i.e User clicks button "Get first book", and a grails controller looks for all files in the assets folder until it finds a match, and then returns the correct one.

Currently, I am loading the files by directly accessing the path. i.e. I am using an explicit aboslute path. According to this post, this is one of the advisable approaches. However, I want to load a file in grails by asking my application to get all assets in the asset folder, instead of using any paths.

My question then is, is it possible to get all files from the assets folder in a simple manner like we can get properties in the yml file (grailsApplication.config.someProperty)


Solution

  • Found the solution here: Grails : getting assets local storage path inside the controller

    The following sample:

    def assetResourceLocator
    
    assetResourceLocator.findAssetForURI('file.jpg')?.getInputStream()?.bytes
    

    worked fine for me.