Tuesday 24 December 2019

How to work with File upload control using Asp.Net – PART - I

Agenda :
            Dear all In this article I would like to demonstrate the essence of  File Upload control. Different properties and uses cases related to the  File upload control. How to work with File upload control using Asp.Net was covered with the help of C#.Net.

Description :
          In previous articles I explained different articles related to C#.Net,Asp.Net,Sql Server,XML and DotNetInterviewQuestions Related articles. Now Let's focus on how to work with File upload control using Asp.Net.



About File Upload Control :
  • File upload control in asp.net was inherited from System.Web.UI.WebControls 
  • File upload control allows the user to browse for the file ensuring that the selected file to be uploaded by providing a browse button
  • Scenarios of using  File upload control were uploading of files/photos in a web pages
Properties File Upload Control :
                 Properties
         Description
FileBytes
It returns byte array that which file has uploaded
FileContent
It returns the stream object which is specific to the file to be uploaded
FileName
It returns the name of the file which was uploaded
HasFile
It returns bool values. Specifies whether the control has a file to upload
PostedFile
This property is responsible for a returns reference to the uploaded file
AllowMultipleFiles
It returns a boolean value. If we set this to true it allows multiple files

About HTPPPOSTEDFILE :

  • Posted File is inherited from the type HttpPostedFile
  • HttpPostedFile  class has the following  important properties
                  Properties
           Description
ContentLength
It returns the size of the uploaded file in bytes.
ContentType
It returns the MIME type of the uploaded file.
FileName
It returns the full fileName


Source Code (Design):


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RegForm.aspx.cs" Inherits="RegForm" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>File Upload Control Example in Asp.Net</title>    
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table class="auto-style1">
            <tr>
                <td>Enter Name</td>
                <td>
                    <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>Enter Qualification</td>
                <td>
                    <asp:TextBox ID="txtQualification" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>Enter CNo</td>
                <td>
                    <asp:TextBox ID="txtCNo" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>Upload Resume</td>
                <td>
                    <asp:FileUpload ID="fupResume" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" />
                </td>
                <td>&nbsp;</td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>



Source Code (Code Behind):


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;
using System.Web.UI.WebControls;

public partial class RegForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if(fupResume.HasFile)
        {
            string fileName = fupResume.FileName;
            fupResume.SaveAs(Server.MapPath("~/Resumes/"+fileName));
            Response.Write("<script>alert('Please Uploaded Successfully !!!')</script>");
        }
        else
        {
            Response.Write("<script>alert('Please Choose Your Resume which is mandatory!!!')</script>");
        }
    }
}

No comments :

Post a Comment