Search This Blog


Check file type of FileUpload control for

This can be achived by
-input type="file" instead of asp:FileUpload
-A server side Textbox Control

Following is a Code snippet of file upload control where I am only accepting the files which has an extension "sas7bdat".If the file extension is not sas7bdat ,I am showing a messge and clear the textbox if the textbox has some value otherwise the selected file name along with the path will come to the textbox.

Code Snippet:

<td class="input-label-left">
Select Data File :
<td class="input-label-left">
<asp:TextBox ID="txtDataFile" runat="server" ToolTip="Select a .sas7bdat file to attach"></asp:TextBox>
<td class="input-label-left">
<input id="fupldDataFile" title="Select a .sas7bdat file to attach" type="file" runat="server" onchange="return load_DataFile();" style="width: 10px; border-right: white 1px solid; border-top: white 1px solid; border-left: white 1px solid; border-bottom: white 1px solid;" />

<script language="javascript" type="text/javascript">
function load_DataFile()

var Filepath = document.getElementById('<%= fupldDataFile.ClientID %>').value;
if(Filepath != "")
var arr1 = new Array;
arr1 = Filepath.split("\\");
var len = arr1.length;
var File1 = arr1[len-1];
var filext = File1.substring(File1.lastIndexOf(".")+1);

if (filext != "sas7bdat")
alert("Invalid File Format..Selected");
h = '<input title="Select a .sas7bdat file to attach" enableviewstate="true" onChange="return load_DataFile();" type="file" style="BORDER-RIGHT: white 1px solid; BORDER-TOP: white 1px solid; BORDER-LEFT: white 1px solid; WIDTH: 10px; BORDER-BOTTOM: white 1px solid" id="ctl00_RMHolder1_TabContainer1_pnlInput_fupldDataFile" name="ctl00$RMHolder1$TabContainer1$pnlInput$fupldDataFile"></td><td width="192" valign="middle">'
document.getElementById('<%= fupldDataFile.ClientID %>').outerHTML =h;
document.getElementById('<%= txtDataFile.ClientID %>').value="";
return false;
document.getElementById('<%= txtDataFile.ClientID %>').value=document.getElementById("ctl00_RMHolder1_TabContainer1_pnlInput_fupldDataFile").value;


tags:How do I Validate the File Type of a File Upload,Check file extension of FileUpload control

No comments: