Hi all –
[U]Situation:
There is also a section to add supporting images, videos etc of the bug/issue using the <input type=”file” /> tag.
Although I could have done server side file size checking, I didn’t want my users to wait half an hour to up load a 10Mb video just to be told it was too large to send via email.
So my client side solution was to have a function fire when a file is selected:
[CODE]<input type=”file” id=”attachment1″ name=”attachment[]” onchange=”findSize(1)” />
The id is name 1 because I have javascript to allow the user to add multiple “Select a file” boxes, dynamically adding 1 to the id number each time. The name is an array for the server side handler of the multiple files (if any). The onchange you see fires “findSize(attachmentRef)”.
[CODE]function findSize(id) {
var maxFileSize = 5242880; //5mb in bytes
var idNumber = id;
var fileInput = $(“#attachment”+idNumber)[0];
if(fileInput.files[0].fileSize > maxFileSize) {
//this file is greater than 5mb – warn the user
alert(“This file exceeds the 5Mb limit – please click Choose File and reselect or compress the file.”);
//set the alert flag so that the user cannot accidentally still upload a large file
document.forms[“send”].elements[“attachmentsGoodToGo”].
value = 0;
}
else {
document.forms[“send”].elements[“attachmentsGoodToGo”].
value = 1;
}
}
This is the findSize function that feeds back to the user instantly if the file is too big for processing – saving headaches all round. I use some jQuery to work out which file input we are looking at and to test the size of the file. If the file is too big, an alert flag is written into the form to prevent the user from hitting submit (one of my client side test conditions for checking the form confirms that this flag is 1 not 0).
[U]Issue:
[U]Solution:
cheers
frank