I am working on a web application which supports file uploading. I am already familiar checking the size in server side, but i wanted to check the file size in a client side.
I know its a browser limitation that we cant access the file properties for security reasons. So i have tried both swfupload and uploadify component. Both are good and serve the needs.
But the limitation is both depends on flash, so if my end user not installed flash then i would end up in a problem. Asking them to install flash is another thing, but its a web portal and the user base is huge. So i don't like the idea of asking them to install flash.
Today i came across file upload functionality in gmail. And tested this in browser(IE) without having flash installed. What i found was interesting. If you upload a big file, they made a postback and immediately returning the message the file size is too large.
How this is possible, how they can find the size of the file without downloading the whole content.? I believe this must be done by reading HTTP header info. am i right?
So this is the exact functionality i wanted to implement. If the flash is installed already, i can use uploadify to check the size otherwise may be i ll implement-the-never-heardof-technique-used-by-google.
can someone recommend me how to do this?
anybody faced the similar problems earlier, what have you done??
Update:
Gmail upload with flash installed in FF
without flash in IE
It is possible to determine the size of the file that is on uploading even before the uploaded file has been transfered completely. Even when using Internet Explorer. By using the HTTP's content-length header!
In java world:
First of all the if you handle the request that arrive to the server in the first servlet/filter/controller before any previous thread mass with it, you'll be albe to get the total file size in the content-length header (request.getContentLength()).
second- have a look in an ajax implementation that prove that it is possible: http://www.ajaxfilebrowser.com/
I'll start off by saying that I haven't been able to generate a working copy and most of this is my understanding from reading around and slight expirience. That said, perhaps you could explore it a bit more and/or set me straight if I'm wrong. The main thing points to the idea of the classic form with a hidden input named MAX_FILE_SIZE... In PHP/Apache, the server will stop if the upload is larger than the MAX_FILE_SIZE (see last post from here). On the PHP side of things, the $_FILES array will return an error code (which can be seen here). Take note of UPLOAD_ERR_FORM_SIZE.
To put this all together, you could have JavaScript upload the file within an IFrame and get the result. If the server spits out an error message (which, in this theory, would be quite quickly), JavaScript can simply alert the user. Otherwise, we can assume the file is now uploaded successfully. Now trying to make sense of Google's code is near impossible; a quick HTML scan is as far as I go and it didn't help any... They do indeed have hidden input fields but none with MAX_FILE_SIZE as the name. They are much shorter and most don't appear to have values. But, I believe this could be possible. Thoughts anyone?
Who knows, perhaps the great Google Web Server is built in with the power to cut off uploads instantly. Thanks for listening!
lol ofc you can do it, it's just a bit of hardwork, if you search for "uploadbar flash" instead of using the data you retrieve to create a uploader you could use it to pass it on or compare or do whatever you want the one i found that seemed quite good is this one, it doesnt necceraly use flash but you could use it to achive the same thing http://www.plupload.com highly configurable p.s. you could force it to just use flash if you really like
I believe file upload is based on RFC 1867, although HTML5 supersedes all this. You can perfectly reimplement this RFC, server side, and check the size of the cumulated incoming stream.
In the most general cases, I don't think you can safely rely on the content-length as it may not represent the size of the file, but just the size of a chunk.
I actually asked this very question (more or less) a few days ago and the general answer seems to be: It can't be done.
For reference: Ensure file size of uploaded file is lower than maxRequestLength before the upload occurs?
You may take a look at the File API which is a draft for HTML 5. Here's a nice article. That's what Gmail uses if the browser supports it of course. But there's no way to ensure this will work across all browsers.