I am new to grails. I am making a web application that uploads an image from the client side and stores it on the server.
My Gsp Code:
<g:uploadForm action="saveImage"> <input type="file" name="image"> <input type="submit" value="Submit"> </g:uploadForm>
My saveImage action in the controller:
def saveImage={ def file = request.getFile('image') if (file && !file.empty) { file.transferTo(new java.io.File("image.jpg")) flash.message = 'Image uploaded' redirect(action: 'uploadImage') } }
In this code, if I load some other files, such as text files, it throws an Exception. To do this, I want to check the file extension, and I want to use the If loop, which ensures that the downloaded file is an image file or not. But I do not know how to find the file extension in grails.
Is there any other way to upload images to grails app. It should only accept image files.
can anyone help?
thanks.
source share