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?

Matan Amir

I know that it is possible to check the size of the file on the server side once it has been completly uploaded by the user, but this is not ideal (e.g. I can always delete the file from the server if it is too big, but it would have been uploaded in teh first place, which I would like to avoid).

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.

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact