How to Limit Upload Size before Uploading!

Can anyone help me in this: How to limit file size before uploading it?
This script works fine, but you have to configure first your browser to make the alert window appear. Which is not good at all This script only runs in IE6/7/8.

function IELimmit(calculate)
{
var myFSO = new ActiveXObject(“Scripting.FileSystemObject”);
var filepath = document.calculate.uploaded_file.value;
var thefile = myFSO.getFile(filepath);
var size = thefile.size;

	if (size > 2097152) {
alert ("Unable to proceed upload, File is larger than 2mb.\

Please upload only files less than 2mb.");
//document.calculate.uploaded_file.value = “”;
//document.calculate.uploaded_file.focus();
return false;
}
return true;
}

---->Any help how to make the alert window appear without any browser configurations??? Thanks in Advance!:wink:

Using a hidden field in the form with name MAX_FILE_SIZE will work for any browser that allows the upload size to be controlled by the browser. eg.

<input type=“hidden” name=“MAX_FILE_SIZE” value=“2097152”>

It can’t be done with JavaScript as JavaScript (unlike JScript used by IE) has no access whatever to files.

Thanks for your reply, but I think I already tried this approach, I want an alert window that informs user that the file they want to upload exceeds the file size limit… any Idea with this?:slight_smile:

The only way t do it is to create your own browser and then drive IE, Firefox, Chrome etc out of business so that everyone uses your browser.

Of course then someone will use the security holes that you have opened up to wipe the hard drives of every computer running the browser.

hey falgall! I want an idea…, and yours is <snip /> just shut up if you have no good ideas to write ok!,

It’s an honest comment though. There is no realistic way to achieve what you’re after, thereby resulting only in unrealistic means. by which to achieve it.

When the impossible has been ruled out, only the improbable remains - Shirlock Holmes (brother)

Sorry guys, but I know there is a way!:stuck_out_tongue:

There isn’t a way. Security features implemented into browsers specifically prohibit JavaScript from being able to find out anything about files.

The only way to even attempt to block large files being uploaded is the hidden field I mentioned earlier - that is the ONLY option that works and then only sometimes.

You must check the filesize on the server immediately after uploading if you want to limit file size. The only way to do that and produce an alert is to do an ajax call to the server after the file has had time to upload to request a file size check and then produce the alert if the uploaded file is too big.