Does anyone know if there is a way to check the size of a file (a picture in my case) that the user can upload from a form *BEFORE* the file is uploaded?
Created May 7, 2012
If this question is not directly Servlet/JAVA related, maybe the answer is Javascript???
-----One obvious solution is to check the HTTP content-length of the file POST request and cancel the upload if it's too big (throw a ServletException for example). The problem is that I think IE doesn't care if the server has stopped receiving the file and continues to upload it anyway.
Hope that helps.
About the Javascript...there *is* a trick I have used to check the file size of a file, but it requires the user to load it locally. You basically create a new Image object with that file name the same way you do with image pre-loaders and check the size property of the Image object. I have used it to get file sizes of images and audio files, but it should work with other types as well.
If I remember right, the Image object loading is asynchronous, so you might need to define an onload function that will be called when the file is actually loaded.
I do think, though, that providing a server-side check is preferable.