Reputation: 3775
I got a GWT application and we need to upload files but i want to limit the size. So currently i have this:
ServletFileUpload.isMultipartContent(request);
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(theFactory);
upload.setFileSizeMax(<2mb>);
upload.setSizeMax(<2mb>);
try {
final List<FileItem> items = upload.parseRequest(request);
for (FileItem fileItem : items) {
<do file stuff>
}
}
catch (FileUploadException e) {
<break and return error>
}
When i upload a 300mb+ file in dev mode as soon as it hits the for loop it breaks and says the file is to big in the exception. This happens a second after I hit the upload button.
When the application is deployed on a tomcat it waits till the file is fully uploaded instead of breaking before and rejecting the file.
I know there is a tomcat parameter but the sizes of files I accept for upload is very different for the different servlets and i want to set this individually for the servlets and not the biggest value which can be much more than the mostly accepted values.
Upvotes: 0
Views: 117
Reputation: 797
You can add a ChangeHandler to your upload button in GWT, and call a native method from it passing the button's element in:
private native boolean isFileSizeValid(Element inputFile)/*-{
var fileToUpdate = inputFile.files[0];
return fileToUpdate.size < 200...0; // pick the right number
}-*/;
This will work fine for single file (if 'multiple' is disabled on the input element). If you allow for uploading multiple files you can iterate through all items of files[]
array and do the math.
This approach will help you to perform all the validation client-side, but needs FileAPI support from the client browser. http://caniuse.com/#feat=fileapi
Sorry, if you were looking for backend validation only.
Upvotes: 1