Josh K
Josh K

Reputation: 28893

Grails resource paths in controller

Say I'm passing a list of image urls from the server to the frontend.

images/
    image1.jpg
    image2.jpg
    image3.jpg

Now I'm just listing the file names, but I need to reference the absolute path (/application/images/image1.jpg). Basically the equivalent of ${resource} server side.

Upvotes: 0

Views: 9270

Answers (3)

Tomáš
Tomáš

Reputation: 2108

for reading file (cvs, xml, img ,...) from controller I use:

def csv = grailsAttributes.getApplicationContext().getResource("/data/jak.csv").getFile()

Regards Tom

Upvotes: 3

Yarovoy
Yarovoy

Reputation: 649

You can call this code in your controller:

String pathToImage1 = g.resource(dir: 'images', file: 'image1.jpg', absolute: true)

Flag 'absolute' may be skipped.

Upvotes: 6

Related Questions