2

I'm trying to find a way to limit the size of a file a user can upload. I need to satisfy the followings:

-I can use client side file size detection but that can be bypassed. So server side is more preferable. -I want to be able to figure out the file size before the upload starts, or if that is not possible, I should be able to set a limit on the size while streaming, so if the stream reaches the limit it stops and doesn't receive the stream from the client anymore. (I saw that Apache has a max limit that I can use, but I was wondering if I can use Spring/Grails to do that)

Is there a way to do this elegantly in Grails ?

P.S: I noticed that "multipartResolver" can set a limit on file uploads, does multipartResolver uploads first and then check or does it check the size before the actual full download of the file ?

AlexCon
  • 1,127
  • 1
  • 13
  • 31

3 Answers3

0

Knowing that you are using your own code for uploading, I am assuming that you have a field in your domain that will save the file. If so, then you could just add a maxSize property in the static constrains of the domain class.

example:

class Files {

  byte[] uploadedFile


static constraints = {
uploadedFile(nullable:true, maxSize: 10 * 1024 * 1024)

}
}

It has worked for me. I hope it helps.

  • I appreciate you offering a solution, but I want to save it into a file system, I don't use database. – AlexCon Dec 27 '13 at 14:57
0

I already answered a similar question here regarding the size of the file. This would be a good use if you are storing the file on a file system rather than as a blob in the database. I do not know when the size of file is checked.

I would assume it would only be able to do this after the file has been fully uploaded. I don't see how you could detect it while streaming as the server won't have all the bytes to calculate the size.

JavaScript cannot detect the size of a file before upload. If you would want to do this client side you would have to look into a flash type solution.

Community
  • 1
  • 1
Gregg
  • 34,973
  • 19
  • 109
  • 214
0

Apache Tomcat allows for the following configuration, In hard-coded or Annotation format. I'm not sure if the max-file-size is calculated during the upload process or after the file is uploaded to a temp place. The documentation indicates the followings :

The @MultipartConfig annotation supports the following optional attributes:

location: An absolute path to a directory on the file system. The location attribute does not support a path relative to the application context. This location is used to store files temporarily while the parts are processed or when the size of the file exceeds the specified fileSizeThreshold setting. The default location is "".

fileSizeThreshold: The file size in bytes after which the file will be temporarily stored on disk. The default size is 0 bytes.

MaxFileSize: The maximum size allowed for uploaded files, in bytes. If the size of any uploaded file is greater than this size, the web container will throw an exception (IllegalStateException). The default size is unlimited.

maxRequestSize: The maximum size allowed for a multipart/form-data request, in bytes. The web container will throw an exception if the overall size of all uploaded files exceeds this threshold. The default size is unlimited.

annotation approach :

@MultipartConfig(location="/tmp", fileSizeThreshold=1024*1024, 
    maxFileSize=1024*1024*5, maxRequestSize=1024*1024*5*5)

and here is the hard-coded value:

<multipart-config>
<!– 50MB max –>
<max-file-size>52428800</max-file-size>
<max-request-size>52428800</max-request-size>
<file-size-threshold>0</file-size-threshold>
</multipart-config>

I appreciate it if anyone can clarify if the MaxFileSize is calculated during the upload process.

AlexCon
  • 1,127
  • 1
  • 13
  • 31