Jquery or Javascript Minimum file upload script

Hi,

Does anyone have/ know a script that stops a user uploading a file that’s size is below 300kb?

I’ve been searching around and all I can find is maximum file size scripts.

Thanks in advance.

Connor

You would check using your server-side the file size and reject if it was not big enough. PHP has a function called filesize() that returns a file size in bytes.

You can do this in modern browsers in JS using HTML5’s File API.

Given this:

<input type="file" id="file">

You can do:

var uploadField = document.getElementById("file");

uploadField.onchange = function() {
    if(this.files[0].size > 307200){
       alert("File is too big!");
       this.value = "";
    };
};

Fiddle

Unless I’m mistaken (which is always possible :)) this means that the file has to be uploaded before you can check the size.

As with form validation, I would implement a check on the client side for those browsers that understand it, as well as a check on the server (as \\.\ suggests) for older browsers, or users with JS disabled.

Yep, the filesize() function of PHP has to have the file uploaded, it is not possible to obtain the filesize from the browser, it is possible to set the Maximum file size with the use of an input field, that has to be before the actual input field for the file selection.

Being reliant on client side checks has never been a good idea but it is a good way of filtering because the majority of users wouldn’t know how to work around that unless they know how to disable JavaScript in the browser.