Applying javascript to check file size and extension
<script type="text/javascript">
function setFileSize() {
var fileEl = document.getElementById('<%=FileUpload1.ClientID%>');
var fileSize = fileEl.files[0].size / 1024 / 1024;
var fileName = fileEl.files[0].name;
var dotPosition = fileName.lastIndexOf(".");
var fileExt = fileName.substring(dotPosition);
if (fileSize > 1) {
fileEl.value = '';
document.getElementById('<%=lblFileStatus.ClientID%>').innerHTML = 'File Should Be less Than 1MB';
return false;
}
}
Rather than relying on the elements them self you should use the given event to the function to get the file(s) that triggered the call to the callback function.
Passing in the event will guarantee you that you are actually working with the current files that caused the function to be called.
For example (I changed the variable name to make it more clear):
ONLINE DEMO HERE
/// pass in event
function checkFile(e) {
/// get list of files
var file_list = e.target.files;
/// go through the list of files
for (var i = 0, file; file = file_list[i]; i++) {
var sFileName = file.name;
var sFileExtension = sFileName.split('.')[sFileName.split('.').length - 1].toLowerCase();
var iFileSize = file.size;
var iConvert = (file.size / 1048576).toFixed(2);
/// OR together the accepted extensions and NOT it. Then OR the size cond.
/// It's easier to see this way, but just a suggestion - no requirement.
if (!(sFileExtension === "pdf" ||
sFileExtension === "doc" ||
sFileExtension === "docx") || iFileSize > 10485760) { /// 10 mb
txt = "File type : " + sFileExtension + "\n\n";
txt += "Size: " + iConvert + " MB \n\n";
txt += "Please make sure your file is in pdf or doc format and less than 10 MB.\n\n";
alert(txt);
}
}
}
This might help
var file = document.getElementById("filex").files[0];
var filename = file.name;
var extSplit = filename.split('.');
var extReverse = extSplit.reverse();
var ext = extReverse[0];
if(!ext === "mp4" || !ext === "flv"){
alert('Accepted');
} else {
alert('Not accepted');
}
you can used this code with file controller in html. any only pass parameter and see the output
<script type="text/javascript">
function AlertFilesize(cid,sz,a){
var controllerID = cid;
var fileSize = sz;
var extation = a;
if(window.ActiveXObject){
var fso = new ActiveXObject("Scripting.FileSystemObject");
var filepath = document.getElementById('fileInput').value;
var thefile = fso.getFile(filepath);
var sizeinbytes = thefile.size;
}else{
var sizeinbytes = document.getElementById('fileInput').files[0].size;
}
var fSExt = new Array('Bytes', 'KB', 'MB', 'GB');
fSize = sizeinbytes; i=0;while(fSize>900){fSize/=1024;i++;}
var fup = document.getElementById('fileInput');
var fileName = fup.value;
var ext = fileName.substring(fileName.lastIndexOf('.') + 1);
if(ext == "gif" || ext == "GIF" || ext == "JPEG" || ext == "jpeg" || ext == "jpg" || ext == "JPG")
{
var fs = Math.round(fSize);
if(fs < fileSize && fSExt[i] == extation)
{
alert("Image Successfully Uploaded");
return true;}else{
alert("Please enter the image size less then"+fileSize+extation);
document.getElementById('fileInput').value='';
return false;
}
}else{
alert("Must be jpg and gif images ONLY");
document.getElementById('fileInput').value='';
return false;}
}
</script>
<input id="fileInput" type="file" onchange="AlertFilesize(this.id,100,'KB');" />