Validate File Upload Control to upload certain extensions


we will take an example to upload images only with this extensions ” jpg , jpeg , png , gif ”

<script type=“text/javascript”>

function checkImageExtension(elem) {

var filePath = elem.value;

if (filePath.indexOf(‘.’) == -1)

return false;

var validExtensions = new Array();

var ext = filePath.substring(filePath.lastIndexOf(‘.’) + 1).toLowerCase();
validExtensions[0] = ‘jpg’;

validExtensions[1] = ‘jpeg’;

validExtensions[2] = ‘png’;

validExtensions[3] = ‘gif’;
for (var i = 0; i < validExtensions.length; i++)

{

if (ext == validExtensions[i])

return true;

else

elem.value = ‘ ‘;

}

alert(ext.toString() + ‘\n’ + ‘Disabled’);

return false;

}

<asp:FileUpload ID=”fuAttachment” runat=”server” />

C#:

protected void Page_Load(object sender, EventArgs e)

{            fuSelectFile.Attributes.Add(“onchange”, “return checkImageExtension(this);”);        }

Screen shot

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s