0

I'm new to grails (1.3.7) and I've been put in a strange situation with displaying an image from the filesystem. The one.png picture is put into web-app/images/fotos directory. The zz.gsp:

<img src="${resource(dir:'images/fotos', file:'one.png')}" alt="Nothing" />

related to the void action def zz = {} works fine. But if I intend to display the same picture in rawRenderImage.gsp:

  <body>
   <p>
     ${fdir}  <br/>   ${fname}      <!-- OK -->
   </p>
   <g:if test="${fname}">
      <img src="${resource(dir:'fdir',file: 'fname')}"   alt ="Nothing"/>
   </g:if>  
  </body>

the picture doesn't appear inspite of the parameters fdir and fname pass to the page. The action in the controller is:

    def rawRenderImage = {
//    def basePath = servletContext.getRealPath("/")
//      def basePath = grailsAttributes.getApplicationContext().getResource("/").getFile().toString() + "/images/fotos/"  
//      def basePath = grailsAttributes.getApplicationContext().getResource("/").getFile().toString()
//      def fname = params.photoId + ".png"
//      def fname = "/images/fotos/" + params.photoId + ".png"

basePath = "images/fotos"        // or basePath=”images” for /images/one.png
fname=”one.png”

        [fdir:basePath, fname:fname]
   }

Even direct assigns basePath=”images/fotos” and fname=”one.png” don't work, as well as any combinations with basePath to obtain the absolute path. Even the case when I put the picture in images directory doesn't work. I use netbeans, but it also doesn't work in console mode.

Help please.

1 Answers1

2

When passing in your filename and directory as variables in the model, don't quote them in your tag's src attribute. Then the Groovy ${} evaluation will evaluate to the variables and not as Strings.

<g:if test="${fname}">
    <img src="${resource(dir:fdir,file:fname)}" alt ="Something"/>
</g:if>
schmolly159
  • 3,881
  • 17
  • 26
  • "${resource(dir:fdir,file: fname)}" -- The same result... Nothing. – tolymark Nov 02 '11 at 20:20
  • That's strange. Have you looked at the generated HTML in your browser to see what url is in the `src` attribute of the ? Also, your example controller code doesn't have a `def` in front of `basePath` or `fname` in the action, although the ones commented out do. – schmolly159 Nov 03 '11 at 14:31
  • You're right with quote, but with the absolute path I have problems. Firebug gives the response from the server to POST rawRenderImage: /home/toly/NetBeansProjects/foto/web-app
    /images/one.png Nothing

    And GET one.png which is http://localhost:8080/foto/home/toly/NetBeansProjects/foto/web-app/images/one.png produces 404 Not Found error... But the image one.png is inside the images folder, and is shown with the relative path.

    – tolymark Nov 03 '11 at 17:35
  • Why are you trying to send the absolute path? Your tomcat server will resolve files relative to the server root and not your filesystem root. If your photos will all be in the directory `images/fotos` with dynamic names, just do `resource(dir:'images/fotos',file:fname)` and pass the filename in the model. – schmolly159 Nov 03 '11 at 21:16
  • You are right. But many posts in internet say it is impossible to use relative pathes in some real servers. And the only solution is with an absolute path obtained with the functions commented out in my post. So I want to know why my grails system does such strange thing. May be it is something with configuration or permissions. In all cases the code presented MUST work, it's correct. This is the question, just by Shakespeare's Hamlet... – tolymark Nov 03 '11 at 21:34
  • Are you sure those posts don't mean relative paths as in `/myWebApp/images/fotos/img.png` instead of `http://localhost:8080/myWebApp/images/fotos/img.png`, which is an absolute path for a web url? The resource tag has an optional attribute of `absolute="true"` if you need an absolute path, but you still would pass parameters like `${resource(dir:'images/fotos',file:fname,absolute:"true"}` – schmolly159 Nov 04 '11 at 20:52
  • Here are some discussions about upload/download in grails, and everything finishes with an absolute path. I don't want to have problems with those processes when I deploy a war file. http://grails.1312388.n4.nabble.com/How-to-set-destination-path-of-quot-transferTo-quot-action-td1366481.html, http://stackoverflow.com/questions/5944393/grails-images-absolute-path, http://stackoverflow.com/questions/3635186/grails-how-to-display-image-in-file-system. Javid Jamae says: “The client can only load the image from an **absolute** or relative URL”. – tolymark Nov 04 '11 at 23:05
  • Absolute URL is a URL with the hostname at the beginning. Absolute file system path is what you're generating in your commented code. They are two different things and a browser can't access an absolute file system path unless the server is set up to serve files from there. Here's a link to a short discussion on file upload/download in Grails: http://grails.1312388.n4.nabble.com/Grails-and-Images-upload-manipulation-visualization-td1318881.html. I'd recommend storing them outside the war and retrieving them via a controller. – schmolly159 Nov 05 '11 at 02:45
  • Thank you very much for your help, schmolly159, I’ll study that discussion. – tolymark Nov 05 '11 at 18:52