{"id":42,"date":"2009-04-16T10:31:25","date_gmt":"2009-04-16T05:01:25","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=42"},"modified":"2016-12-19T15:28:09","modified_gmt":"2016-12-19T09:58:09","slug":"grails-upload-and-render-file","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/grails-upload-and-render-file\/","title":{"rendered":"Grails : Upload and Render File"},"content":{"rendered":"<p><meta http-equiv=\"CONTENT-TYPE\" content=\"text\/html; charset=utf-8\" \/> \t<title><\/title> \t<meta name=\"GENERATOR\" content=\"OpenOffice.org 2.4  (Linux)\" \/><\/p>\n<style type=\"text\/css\"> \t<!-- \t\t@page { size: 21cm 29.7cm; margin: 2cm } \t\tP { margin-bottom: 0.21cm } \t--> \t<\/style>\n<p>Lets discuss how can we upload a file and store it in a database, along with that how do we render it on the gsp page. I am taking an example to upload and render the image in this blog, though the code would be same for any other file.<\/p>\n<p>So\u00a0Lets consider I have a domain class with the name Person<\/p>\n<pre lang=\"groovy\">\r\nclass Person {\r\n    .\r\n    .\r\n    .\r\n    bytes[] picture\r\n    static constraints = {\r\n        picture(size:0..5000000)  \/\/ to store files upto 5MB approx\r\n    }\r\n}<\/pre>\n<p>Once we have a field with a byte[] defined in the class then automatically its view is generated with a input tag of file type in html, which returns the MultipartFile object in the controller. MultipartFile provides various useful methods which can be used getBytes(), getSize(), getContentType(), getName() etc.<\/p>\n<p>So as MultipartFile object returs the byte[] which is to be stored in a picture property of a person class, in the save action of the PersonController Lets see how we do it :<\/p>\n<pre lang=\"groovy\">\r\nperson.picture = params.picture\r\nor\r\nperson.properties = params\r\n\r\n\/\/Now the person object holds the image and lets save it\r\n\r\nperson.save()<\/pre>\n<p>We are done with file uploading. Now to render the image\/file, lets have a separate controller in a controller directory, where we would define the action to generate the image:<\/p>\n<pre lang=\"groovy\">\r\nclass ImageController {  def defaultAction ='show'\r\n\r\ndef show= {\r\n  \u00a0 \/\/loads the class with a name and assigns obj a new instance created of the same object\r\n    def obj = Class.forName(\"${params.classname}\",true,Thread.currentThread().contextClassLoader).newInstance();\r\n\u00a0   def object = obj.get( params.id )\r\n    response.setContentType(params.mime)\r\n\u00a0   byte[] image = object.\"${params.fieldName}\"\r\n\u00a0   response.outputStream << image\r\n\u00a0 }\r\n}<\/pre>\n<p>Now where ever we want to display the image, we can just include the following tag:<\/p>\n<pre lang=\"groovy\">\r\n<\/pre>\n<p>Note : The above code can be dangerous as a hacker can modify the className and feildName in the query string and get the hidden information.<br \/>\nHope this helped.<\/p>\n<p>~Amit Jain~<\/p>\n<p>amit@intelligrape.com<\/p>\n<p><a href=\"http:\/\/www.tothenew.com\/blog\/\" onclick=\"javascript:pageTracker._trackPageview(&#039;\/outbound\/article\/www.IntelliGrape.com&#039;);\">http:\/\/www.IntelliGrape.com\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lets discuss how can we upload a file and store it in a database, along with that how do we render it on the gsp page. I am taking an example to upload and render the image in this blog, though the code would be same for any other file. So\u00a0Lets consider I have a [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":25},"categories":[7],"tags":[72,71,4840,73,70,74],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/42"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=42"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/42\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=42"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=42"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=42"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}