Grails: displaying created image in gsp

前端 未结 8 2002
伪装坚强ぢ
伪装坚强ぢ 2020-11-29 10:07

I\'m very new to Grails so there\'s probably a very simple answer to this question. I\'m trying to display a dynamically created image in a gsp. The image is NOT stored in

8条回答
  •  一整个雨季
    2020-11-29 10:47

    If you write the bytes to the output stream, you can treat the controller/action as the source of the image in your GSP. Here's a quick, untested example:

    // controller action
    def displayGraph = {
        def img // byte array
        //...
        response.setHeader('Content-length', img.length)
        response.contentType = 'image/png' // or the appropriate image content type
        response.outputStream << img
        response.outputStream.flush()
    }
    

    You could then access your image in the src of an tag like this:

    
    

    Update:

    After reading your question again, this may or may not work - it looks like you might be displaying the graph as the result of a form submission. This will only work if you're storing the state on the server somewhere (instead of just getting it from the one request where the form is submitted). If you do store enough state on the server to generate the graph, then you'd have to provide some additional parameters to your controller to get the correct image, e.g. src="${g.link(controller: 'myController', action: 'displayGraph', params: ['id': 1234])}", where id is how you retrieve the graph state.

提交回复
热议问题