Reputation: 2675
I need to display image that is in file system not in project directory in grails..
i used
< img src="${createLinkTo(dir: '/Users/ME/ddImages/reg/', file: '${userInstance.fileName}.jpg')}" alt="User Photo"/>
but that doesn't seem to work
so any help?
Upvotes: 0
Views: 3430
Reputation: 2340
Think you are trying to render a file that is not in the webroot of your project. You CAN'T do this by simply passing a URL to your view. One way you can do it is read the image file as a stream and then write the byte stream to view. e.g:
<img src="${request.contextPath}/controllerName/getImage?filepath=${path_to_file_in_filesystem}" alt="whatever. alternatively, filename"/>
then the action in the controller will look like this:
def getImage(){
def path = params.filepath
//returns an image to display
BufferedImage originalImage = ImageIO.read(new File(path));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
def fileext = path.substring(path.indexOf(".")+1, path.length())
ImageIO.write( originalImage, fileext, baos );
baos.flush();
byte[] img = baos.toByteArray();
baos.close();
response.setHeader('Content-length', img.length.toString())
response.contentType = "image/"+fileext // or the appropriate image content type
response.outputStream << img
response.outputStream.flush()
}
That should do it. Hope this helps someone.
Upvotes: 0
Reputation: 33345
BTW, the grails tag is depricated, use resource; and that tag will keep you within the application's base path
http://www.grails.org/Tag+-+createLinkTo
Documentation on the img tag http://www.w3schools.com/tags/att_img_src.asp,
Possible values for "src"
An absolute URL - points to another web site (like src="http://www.example.com/image.gif")
A relative URL - points to a file within a web site (like src="image.gif")
Upvotes: 2