1

I'm reading this tutorial on file uploads: https://guides.grails.org/grails-upload-file/guide/index.html

But even if the file size limit is set to 25MB, there is no description of how to handle the FileUploadBase$SizeLimitExceededException exception when the file uploaded is greated than that. If there is a limit, even if it's too big, someone will hit it and would like to show a nice error to the user, but the exception seems to happen before executing my controller's code.

The question is: how to handle that kind of exception on a Grails 3.3.x app?

Pablo Pazos
  • 3,080
  • 29
  • 42
  • See James Kleeh's answer at https://stackoverflow.com/questions/29845943/grails3-file-upload-maxfilesize-limit. – Jeff Scott Brown Oct 15 '19 at 10:25
  • is that answer from 2015 valid for Grails 3.3.10? – Pablo Pazos Oct 15 '19 at 15:29
  • Pablo did you ever figure this out? I am having a problem with Grails 3.2.12. I can check the exception using James Kleeh's answer but the servlet seems to start looping. – Joe Sep 12 '21 at 13:41
  • There is no way of doing this from Grails. Basically all answers say to increase the limit, and there is no answer about actually catching the exception and handling it gracefully from a controller. I guess since this is really handled by Spring not Grails, some Spring workaround should be possible, but that escapes my knowlegde. – Pablo Pazos Sep 12 '21 at 17:44

1 Answers1

0

I think after 2MB file size the James Kleeh solution has issues with Tomcat max-swallow-size causing multiple exceptions.

https://github.com/grails/grails-core/issues/9378

I went ahead and used Javascript and at least did a client side validation. See below:

function validateSize() {
    var fileSize = document.getElementById("uploadFile").files[0];
    var sizeInMb = (fileSize.size / 1024) / 1024;
    var sizeLimit = 10;
    if (sizeInMb > sizeLimit) {
        alert('File size must be less than 10 MB');
        return false;
    }
    showSpinner();
    return true;
}

<input type="submit" value="I Accept" class="btn btn-success" onclick="return validateSize()"/>
Joe
  • 1,219
  • 8
  • 13
  • I did the same, though client side validation doesn't solve the real issue, just a palliative that can be easily overridden. – Pablo Pazos Sep 16 '21 at 19:19