Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking the size of file before uploaing it

Tags:

javascript

Is there is any way to check file size before uploading it using javascript?

like image 451
Niraj CHoubey Avatar asked Jan 22 '23 02:01

Niraj CHoubey


2 Answers

Only on some browsers. There's a new API from the W3C that allows very limited access to the file system from client-side Javascript in the browser. It works by having the user explicitly allow you access to the file by selecting it in an <input type='file'> field. You can then query its properties (not including its full path) and even read it directly without sending it to the server.

The File API was originated by Mozilla and is supported by Firefox 3.6 upward (earlier versions supported an earlier version of it). Chrome and Safari also have it, I believe. Here's an article about it by Arun Ranganathan from Mozilla.

So you could try to use it, and if it's there, great; if not, you're stuck with not being able to do this on the client.

Naturally you will want to impose any file size restrictions on the server as well, regardless of whether you can do it on the client. Client-side validation is always a convenience, never a substitute for defending your server.

like image 140
T.J. Crowder Avatar answered Feb 01 '23 14:02

T.J. Crowder


No - javascript does not have access to the file system. If you can use a flash or silverlight gadget to do your uploads, you would be able to do this.

like image 30
Ray Avatar answered Feb 01 '23 15:02

Ray