Add Filter to File Upload Control using Java Script

Ranjeet_8
Posted by Ranjeet_8 under JavaScript category on | Points: 40 | Views : 1620
Add this Java script on your .ASPX page

<script type="text/javascript" language="javascript">
function CheckForTestFile() {
var fuID = document.getElementById('<%=FileUpload1.ClientID%>');
var FileName = fuID.value;
//Checking for file browsed or not
if (InputTrim(FileName) == '') {
alert("Plz select a file!!!");
fuID.focus();
return false;
}
// Set d extension array here (File Type)
var FUextArray = new Array(".txt", ".doc", ".docx", ".csv", ".xls",
".htm", ".html");
while (FileName.indexOf("\\") != -1)
FileName = FileName.slice(FileName.indexOf("\\") + 1);
var FUext = FileName.slice(FileName.indexOf(".")).toLowerCase();

// Searching extension with given extensions (File Type).
for (var i = 0; i < FUextArray.length; i++) {
if (FUextArray[i] == FUext) {
return true;
}
}
// Alert error msg here
alert("Please upload files only : "
+ (FUextArray.join(" ")) + "\n");
fuID.focus();
return false;
}
// Trim d input file name
function InputTrim(input) {
var ITr = /^\s*/;
var ITrr = /\s*$/;
input = input.replace(ITr, "");
input = input.replace(ITrr, "");
return input;
}
</script>

Add this Control on your .ASPX page

<asp:FileUpload ID="FileUpload1" runat="server" onchange="return CheckForTestFile();">

Comments or Responses

Login to post response