Search code examples
grailssvggrails-2.0grails-controller

Render and display an SVG-File with a Grails Controller


I want to create a table that contains svg-images. These images are generated after processing the table data.

My controller looks like this:

def render(){
    byte[] array =  SvgExporter.export(imgsource)       
    render file: array, fileName: "my.svg", contentType: 'svg'
    }

under grails.mime.types in config.groovy, I defined

svg: 'image/svg+xml'

the table entry on gsp looks like this:

<img src="${createLink(controller: 'testview', action: 'render')}">

The funny thing now is that it works if I use PNG instead of SVG, the image gets displayed.

When I use the above code with SVG, the image does not get displayed on the page. When I call the controller directly, the correct svg-image gets downloaded. Any ideas?


Solution

  • You are sending the wrong mimetype down with the render. instead use:

    render contentType: 'image/svg+xml', ...
    

    The mappings in grails.mime.types are not meant for you to shortcut them, but are used internally in grails.