1

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?

IgniteCoders
  • 4,834
  • 3
  • 44
  • 62

2 Answers2

1

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.

IgniteCoders
  • 4,834
  • 3
  • 44
  • 62
0

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

Mario
  • 4,784
  • 3
  • 34
  • 50
Rahul Mahadik
  • 11,668
  • 6
  • 41
  • 54
  • Note: Working with IntelliJ IDEA 2016/2017/2018 :) – Rahul Mahadik Apr 11 '18 at 18:57
  • @IgniteCoders what is the size of file, which you are trying to upload? – Rahul Mahadik Apr 12 '18 at 11:58
  • I am trying to upload different size files. between 10 and 2 Mb. But i solved it finally, look at my answer. – IgniteCoders Apr 12 '18 at 12:00
  • 1
    I think i also mentioned same :) check `maxFileSize: 26214400 and maxRequestSize: 26214400` – Rahul Mahadik Apr 12 '18 at 12:02
  • Yea, but i never thought that could be the problem. Because i didnt get any error, the config just take default and skiping the mine. – IgniteCoders Apr 12 '18 at 12:06
  • and how to deal with exceptions if file is bigger than the limit? – Pablo Pazos Oct 15 '19 at 00:09
  • @PabloPazos Grails default size for file uploads is 128000 (~128KB). When this limit is exceeded you’ll see the exception: `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` – Rahul Mahadik Oct 15 '19 at 05:43
  • yep, but that exception AFAIK is thrown before the controller, so where to do the catch? – Pablo Pazos Oct 15 '19 at 15:28