Grails: displaying created image in gsp

前端 未结 8 1944
伪装坚强ぢ
伪装坚强ぢ 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:43

    my suggestion actually has two parts.

    1) Use the solution recommend by Rob above to generate the chart artifact, however save it to a static file with a unique identifier that is passed back as part of the response from the form submit, then rendering the chart is no different then rendering any other image.

    i would suggest that the identifer be constructed from the specifif params passed in on the form submit so they can be used as a caching mechanism to render the chart again without rebuilding it

    2) create a service, maybe a Quartz Service, that periodically cleans up the static charts that were created for the application

    0 讨论(0)
  • 2020-11-29 10:46

    The following code works in Grails 2.x.

    HomeController.groovy

    class HomeController {
    
        def index() {
        }
    
    
        def viewImage(){
            def file = new File(params.title)
            def img = file.bytes
            response.contentType = 'image/png' // or the appropriate image content type
            response.outputStream << img
            response.outputStream.flush()
        }
    }
    

    views/home/index.jsp

    <%@ page contentType="text/html;charset=ISO-8859-1" %>
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
        <meta name="layout" content="main"/>
        <title>View Image</title>
    </head>
    <body>
      <div class="body">
      <img src="<g:createLink controller="home" action="viewImage"
              params="[title: 'C:/pictures/myimage.png']"/>"/>
      </div>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-11-29 10:46

    I used FileUploadService to save the Image file In Grails .If you getting images from directory means, try this:

    <img style="height: 120px;width: 102px;"src="${resource(dir:'personImages', file:    personalDetailsInstance.id + '.png')}" />
    
    0 讨论(0)
  • 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 <img> tag like this:

    <img src="${createLink(controller: 'myController', action: 'displayGraph')}"/>
    

    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.

    0 讨论(0)
  • 2020-11-29 10:47

    I believe it's not about Grails but about HTML. You could:

    1. Make the dedicated action that pipes image accept certain parameters, and generate the image in that action;
    2. Embed it base64-encoded into HTML, like here:

      <img src="data:image/gif;base64,R0lGODlhUAAPAKIAAAsL...etc..." alt="wow" />

    0 讨论(0)
  • 2020-11-29 10:59

    Just an improvement to @Rob's answer:

    Another option which is a bit nicer, you can just render the image, straight from your controller's action:

    // controller action
    def displayGraph = {
        def img // a byte[], File or InputStream
        render(file: img, contentType: 'image/png')
    }
    

    See also: http://grails.org/doc/latest/ref/Controllers/render.html

    0 讨论(0)
提交回复
热议问题