Upload File Size Validation check without doing a page refresh

Is there a AJAX based solution or PHP based solution to check the filesize a user is trying to upload via a form.
If the filesize exceeds more than 2 MB we want to give a message to the user that they need to upload a smaller file size.

There are tons of Active X/Php solutions.

We want to avoid Active X

And when using PHP it refreshes the page, is there anyway we can check filesize on the fly without having to refresh the page.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

just had a quick look. not sure if you could add something into a function with jquery plugin like this one.

<input type="file" name="somename" size="size in bytes">

You can use this to restrict the size. Unfortunately, you can not do this on client side without activex or signed applets. This is one of the security features.
Richard QuadlingSenior Software DeveloperCommented:
From my understanding of Javascript, you have no access to the files on the local harddrive.

Therefore, JS cannot send anything to the server about the requested file.

The reason you see the ActiveX components is that these are providing JS access to the file system (in some form and to various degrees).

But, having said all of that, something you could try is to use a packet sniffer to see exactly what is being sent by these ActiveX components and see if any of it can be replicated. I doubt you will get anything.

Thinking more about this, just imagine the security issues.

If you COULD use JS to AJAX a file from the local pc to the server, the nasty people would have a field day.

So, I would say that without the user loading a proprietary plugin, you are not going to be able to do this using native code (js/dom/etc).
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

It can be done using flash, which although IE loads using ActiveX i wouldn't classify it as you typical activex control and its available in other browsers i.e Firefox through NPAPI, and according to Adobe is the most widely used piece of software to date, I think 300,000,000 for flash player 8.

You can find alot of examples if you do a search in google.



RondittmannAuthor Commented:
I need a solution which will check the filesize on the fly, and notify the end user if the size is larger than 2MB.

The last flash solution has an entire component for multi file upload. I just need 1 upload field, we cannot deploy the component in its current state.

Whatever method we deploy it also cannot be in Iframes.

Richard QuadlingSenior Software DeveloperCommented:
With regard to PHP, the $_FILES super global array is not populated until after the file upload has taken place.

So, as it stands (5.2.x), you cannot get the size of the upload until the upload has taken place.

The HTTP POST command does send a Content-length field.

Here is a grab from one of a test upload (see snippet below).

The actual file I sent is 29,090,344 bytes, so there is an overhead for the multipart.

The amount of overhead is dependent upon the filename and filetype and what boundary is used (that may be fixed, but different browsers may send different things).

I believe that PHP6 has some work on allowing you to get to the header before the main PHP code is run. Not exactly sure how.

I can't find the exact message on internals, maybe you can.

POST /indiv/franklin/cgi-bin/values HTTP/1.1
Host: www.oac.uci.edu
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9b2) Gecko/2007121120 Firefox/3.0b2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.7,en-us;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://hydra.nac.uci.edu/indiv/franklin/doc/file_upload.html
Content-Type: multipart/form-data; boundary=---------------------------265001916915724
Content-Length: 29090558
Content-Disposition: form-data; name="userfile"; filename="sp30767.exe"
Content-Type: application/x-msdos-program

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CWS (haripriya)Commented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup topic area:
   Accept: RQuadling {http:#20879398}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

Experts Exchange Cleanup Volunteer
Forced accept.

EE Admin
i know this is a bit late, but i came across this, which seems to validate the size before upload
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.