Grails 3 upload file limit size

936 Views Asked by At

What i am doing and what happened?


I am trying to upload files in grails and dowloand them. After make it, I am still facing a problem, when the file size is large. Here is the exception:

Caused by: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: 
the request was rejected because its size (3553808) exceeds the configured maximum (128000)

What I tryed and the result:


I found this asked before in this question, and the answer is to put some configuration variables:

grails:
    controllers:
        upload:
            maxFileSize: (10 * 1024 * 1024)
            maxRequestSize: (10 * 1024 * 1024)

But still getting the same error. I also tryed to add some dependencies as said here. Or close IDE And rebuild. And nothing could be solved.


Did someone face this issue and could solved it?

2

There are 2 best solutions below

2
On BEST ANSWER

The problem is in the assignation of the cofig variables. I assign them without operators:

        maxFileSize: 10485760
        maxRequestSize: 10485760

Instead of:

        maxFileSize: (10 * 1024 * 1024)
        maxRequestSize: (10 * 1024 * 1024)

This is how I solved the problem.

8
On

Following working fine for me with grails version 3.2 and above

We are going to allow 25MB files uploads.

25 * 1024 * 1024 = 26.214.400 bytes

My /grails-app/conf/application.yml

grails:
 controllers:
  upload:
    maxFileSize: 26214400
    maxRequestSize: 26214400

My my.gsp file

<g:form action="saveImage" enctype="multipart/form-data" method="POST">

    <input type="file" placeholder="Select file" name="file">

    <g:submitButton value="Upload File"/>

</g:form>

My controller action

def saveImage(){
def fileName = ''
def uploadedFile = request.getFile('file')

if (uploadedFile)
    if (!uploadedFile.empty) {
        try {

            int dot = uploadedFile.originalFilename.lastIndexOf('.');
            def fileExt = uploadedFile.originalFilename.substring(dot + 1);
            fileName = ("myFile." + fileExt).toString()
            def basePath = ''
            if (Environment.current == Environment.PRODUCTION) {
                basePath ='/var/local/prj/uploads/' // this works with production and tested on Ubuntu OS
            } else {
                basePath = grailsApplication.mainContext.servletContext.getRealPath('/uploads/') // this will take your project directory path Project\src\main\webapp\uploads folder
            }
            uploadedFile.transferTo(new File(basePath + fileName))
        } catch (Exception e) {
            e.printStackTrace()
        }
    }
    // Your redirect code here
}

Download file action

def file = new File("Your file path")
if (file.exists()) {
    response.setContentType("application/octet-stream") // or or image/JPEG or text/xml or whatever type the file is
    response.setHeader("Content-disposition", "attachment;filename=\"${file.name}\"")
    response.outputStream << file.bytes
} else 
    render "File does not exist!"

Hope this helps you