Introduction:
Here I will explain how to validate uploaded file extension from fileupload in asp.net to using c#, vb.net with example or allow only image format files to upload in asp.net fileupload example. By using “Path.GetExtension” properties we can get uploaded file extension and allow only image format files to upload from fileupload control in asp.net using c#, vb.net.
Description:
In previous articles I explained convert datatable to list example using LINQ in C#, insert, update and delete gridview using single stored procedure in asp.net, display images from database in image control in asp.net, Asp.net mvc project example with demo, show multiple markers in google map from database in asp.net and many articles relating to file upload, gridview, asp.net, c#,vb.net and jQuery. Now I will explain how to validate uploaded files from fileupload control in asp.net using c#, vb.net with example.
In previous articles I explained convert datatable to list example using LINQ in C#, insert, update and delete gridview using single stored procedure in asp.net, display images from database in image control in asp.net, Asp.net mvc project example with demo, show multiple markers in google map from database in asp.net and many articles relating to file upload, gridview, asp.net, c#,vb.net and jQuery. Now I will explain how to validate uploaded files from fileupload control in asp.net using c#, vb.net with example.
To
validate uploaded files from fileupload control we need to write the code like
as shown below
string strpath = System.IO.Path.GetExtension(fileupload1.FileName);
if (strpath != ".jpg"
&& strpath != ".jpeg"
&& strpath != ".gif"
&& strpath != ".png")
{
lblExtension.ForeColor = System.Drawing.Color.Red;
lblExtension.Text = "Only image
formats (jpg, png, gif) are accepted ";
}
else {
lblExtension.ForeColor = System.Drawing.Color.Green;
lblExtension.Text = "Uploaded
Successfully";
}
|
If
you want to check it in complete example open your aspx page and write the
following code
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Create, Write and Read
Cookies in Asp.net using C#, VB.NET</title>
</head>
<body>
<form id="form1"
runat="server">
<div>
<asp:Button ID="btnCreate"
runat="server"
Text="Create
Cookies" onclick="btnCreate_Click" />
<asp:Button ID="btnGet"
runat="server"
Text ="Get
Cookie Values" onclick="btnGet_Click" /><br /><br />
<asp:Label ID="lblResult"
runat="server"/>
</div>
</form>
</body>
</html>
|
After completion of aspx page add following namespaces in
codebehind
C#
Code
using System;
|
After completion of adding namespaces you need to write the
code like as shown below
protected void btnGet_Click(object
sender, EventArgs e)
{
string strpath = System.IO.Path.GetExtension(fileupload1.FileName);
if (strpath != ".jpg"
&& strpath != ".jpeg"
&& strpath != ".gif"
&& strpath != ".png")
{
lblExtension.ForeColor = System.Drawing.Color.Red;
lblExtension.Text = "Only image
formats (jpg, png, gif) are accepted ";
}
else {
lblExtension.ForeColor = System.Drawing.Color.Green;
lblExtension.Text = "Uploaded
Successfully";
}
}
|
VB.NET
Code
Imports System.Web
Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As
Object, e As
EventArgs)
End Sub
Protected Sub btnGet_Click(ByVal
sender As Object,
ByVal e As
EventArgs)
Dim strpath As String =
System.IO.Path.GetExtension(fileupload1.FileName)
If strpath <> ".jpg"
AndAlso strpath <> ".jpeg" AndAlso
strpath <> ".gif" AndAlso strpath <> ".png"
Then
lblExtension.ForeColor = System.Drawing.Color.Red
lblExtension.Text = "Only image
formats (jpg, png, gif) are accepted "
Else
lblExtension.ForeColor = System.Drawing.Color.Green
lblExtension.Text = "Uploaded
Successfully"
End If
End Sub
End Class
|
Demo
Please check below demo to validate uploaded files from
fileupload control to allow only images in asp.net
using c#, vb.net.
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
3 comments :
nice one.
good
hi,
My requirement is something different.. i have done this in my module, but my client asked me don't allow any file to upload just by extension. bcoz it may possible that any .exe/.apk or other files can be renamed to extension .jpg/.png and uploaded.. googling for the same, looking for help.. Thanks in advance.
Note: Only a member of this blog may post a comment.