Programmatic File Uploads

    The tag conveniently adds the enctype="multipart/form-data" attribute to the standard <g:form> tag.

    There are then a number of ways to handle the file upload. One is to work with the Spring MultipartFile instance directly:

    1. def upload() {
    2. if (f.empty) {
    3. flash.message = 'file cannot be empty'
    4. render(view: 'uploadForm')
    5. f.transferTo(new File('/some/local/dir/myfile.txt'))
    6. response.sendError(200, 'Done')
    7. }

    This is convenient for doing transfers to other destinations and manipulating the file directly as you can obtain an InputStream and so on with the interface.

    File Uploads through Data Binding

    File uploads can also be performed using data binding. Consider this Image domain class:

      It’s important that you set the or maxSize constraints, otherwise your database may be created with a small column size that can’t handle reasonably sized files. For example, both H2 and MySQL default to a blob size of 255 bytes for byte[] properties.

      It is also possible to set the contents of the file as a string by changing the type of the myFile property on the image to a String type:

      Increase Upload Max File Size

      Grails default size for file uploads is 128000 (~128KB). When this limit is exceeded you’ll see the following exception:

      1. org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException

      You can configure the limit in your application.yml as follows:

      = The maximum size allowed for uploaded files.

      maxRequestSize = The maximum size allowed for multipart/form-data requests.

      You should keep in mind OWASP recommendations - Unrestricted File Upload

      These limits exist to prevent DoS attacks and to enforce overall application performance