Wednesday 25 December 2019

Working with File Upload control using Asp.Net – PART - II

Agenda :
          Dear All, In this article I'm demonstrating "Working with file upload control in asp.net – PART - II".Working with FileUpload Control using Asp.Net is quite simple. As per the coding pattern, we can achieve the uploading of files that can be achieved with a lesser number of lines of code which can be demonstrated in the earlier tutorial named "How to work with File upload control using Asp.Net".


 However, please be notice that there were few concerns we can even strengthen the earlier implementation with multiple checks so that it should be more qualitative in real-time production environments which can be discussed in this tutorial named as "Working with file upload control in asp.net – PART - II" this example was done with C#.Net using Asp.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.


Thinks to be Taken Care in File Upload in Part - II :

  • We must check the type of file which is being uploaded
  • Size of the File which is being uploaded
  • So that Junk can be handled before hitting to the server
  • The above checks really bring potential value to the implementation. Please be ensure that your application is in specifically dealing with certain types of files for e.g.., jpg/png/doc/Docx/Xls/txt, etc of a certain size

Design (Code):


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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>File Upload Part - II By PNV Technical Hub</title>
    <style type="text/css">
        .auto-style1 {
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <table class="auto-style1">
            <tr>
                <td>Enter Name</td>
                <td>
                    <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>Enter RNo</td>
                <td>
                    <asp:TextBox ID="txtRNo" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>Upload Pic</td>
                <td>
                    <asp:FileUpload ID="fupProfilePic" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" />
                </td>
                <td>&nbsp;</td>
            </tr>
        </table>
    </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 _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (fupProfilePic.HasFile)
        {
            try
            {
                if (fupProfilePic.PostedFile.ContentType == "image/jpeg")               
                {
                    if (fupProfilePic.PostedFile.ContentLength < 102400)
                    {
                        string filename = fupProfilePic.FileName;
                        fupProfilePic.SaveAs(Server.MapPath("~/ProfilePic/") + filename);
                        Response.Write("<script>alert('Image uploaded successfully ....')</script>");                       
                    }
                    else
                        Response.Write("<script>alert('Please upload Image file less than 100 KB !!!')</script>");
                }
                else
                    Response.Write("<script>alert('Please upload Image files Only')</script>");
            }
            catch (Exception ex)
            {
                Response.Write("<script>alert('Fail in Uploading of File.Due to this error :: ''" + ex.Message + "')</script>");
            }
        }
        else
        {
            Response.Write("<script>alert('Please select a Image for upload ....')</script>");
        }
    }
}

No comments :

Post a Comment