Category >
ASP.NET
|| Published on :
Monday, November 11, 2019 || Views:
5375
||
file upload validation fileupload required validation
Here Pawan Kumar will explain how to do fileupload required validation in asp.net using required field validator control
Method 1 - fileupload required validation in asp.net using required field validator control
- Step 1 - Create a new asp.net website using Visual Studio 2019 or any other version
- Step 2 - Add a new Webform C# into the website we have create in previous step.
- Step 3 –Now add the below codes to achive “ required validation on file upload control in asp.net“ functionality
DEMO VIDEO
fileuploadaspnet.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="fileuploadaspnet.aspx.cs" Inherits="fileupload_fileuploadaspnet" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net FileUpload validation example: how to validate FileUpload control (file upload)</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>asp.net FileUpload example: file upload validation in asp.net c#
</h2>
<asp:Label ID="Label1" runat="server">
</asp:Label>
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="Choose a file for upload it." Font-Bold="true">
</asp:Label>
<br />
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:RequiredFieldValidator
ID="RequiredFieldValidator1" runat="server" ControlToValidate="FileUpload1" ErrorMessage="Choose a file!">
</asp:RequiredFieldValidator>
<br />
<asp:Button ID="Button1" runat="server" Font-Bold="true" OnClick="Button1_Click" Text="Upload Now" />
<br />
<br />
</div>
</form>
</body>
</html>
fileuploadaspnet.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class fileupload_fileuploadaspnet : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string uploadFolder = Request.PhysicalApplicationPath + "\\Upload\\";
FileUpload1.SaveAs(uploadFolder + FileUpload1.FileName);
Label1.Text = "File uploaded successfully: " + FileUpload1.PostedFile.FileName;
}
}
We have also added the source code for file upload in asp.net with validation. You can freely use these source codes.
Method 2 - fileupload validation in asp.net using javascript
In method two We will shows you how to validate a file extension in ASP.NET using JavaScript.
Now create a JavaScript function to ensure that only the specified extension is valid. First of all you start Visual Studio .NET and make a new ASP.NET web site using Visual Studio 2019.
Below is the code for fileupload required validation in asp.net using javascript
- Step 1 - Create a new asp.net website using Visual Studio 2019 or any other version
- Step 2 - Add a new Webform C# into the website we have create in previous step.
- Step 3 –Now add the below codes to achive “fileupload control in asp.net javascript validation “ functionality
fileupload-method-2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="fileupload-method-2.aspx.cs" Inherits="fileupload_method_2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function validate() {
var array = ['pdf', 'doc', 'docx', 'txt', 'xlsx', 'ppt', 'zip'];
var xyz = document.getElementById("FileUpload1");
var Extension = xyz.value.substring(xyz.value.lastIndexOf('.') + 1).toLowerCase();
if (array.indexOf(Extension) <= -1) {
alert("Please Upload only pdf,doc,zip,txt.xlsx and ppt extension flle");
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
</div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return validate();" />
</form>
</body>
</html>
Hope, Both these two methods will help you provide file upload required features as well as provide the client side validation for fileupload asp.net control.
Download Source Codes