google-site-verification=ECX1M6_Vb39ty4VtQDXQce6wOjmPTxSettd3hTIZb9Q

Verification and Validation Upload File Type and File Size - ASP .Net

random

Verification and Validation Upload File Type and File Size - ASP .Net

Check file size and file type before upload - with C#


In the previous articles I explained about How to Call Asp.Net WebMethod using Ajax jQuery in Web Application?, C# GridView - How to change Cell Color based on different values? and many more.

In this post I will explain the functionality to upload the image by using asp:FileUpload control in asp .net, before uploading the file if you want to check or validate the file extension, file size and the allow to upload. Find the source code below:-

Check file size and file type before upload,check a uploaded file type and file size


Default.aspx
<%@ 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>Check file size and file type before upload - with C#</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>

            <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                  Browse Image: -  <asp:FileUpload runat="server" ID="upFile" CssClass="EU_FileUpload" />

                </ContentTemplate>
            </asp:UpdatePanel>
            <br />
            <asp:Button ID="btnSave" runat="server" Text="Validate" OnClick="btnSave_Click" />

            <asp:Label ID="lblMsg" runat="server" Text=""></asp:Label>
        </div>
    </form>
</body>
</html>

Default.aspx.cs
using System;
using System.Collections.Generic;
using System.IO;
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)
    {
        lblMsg.Text = string.Empty;
    }
   
    protected void btnSave_Click(object sender, EventArgs e)
    {       
        ValidateFileUpload();
    }

    // validating file size
    public void ValidateFileUpload()
    {
        try
        {
            if (upFile.HasFile)
            {
                Int32 maxFileSize = 1024000;
                Int32 fileSize = upFile.PostedFile.ContentLength;

                if (fileSize > maxFileSize)
                {
                    lblMsg.Text = String.Format("Max. File size exceeded {0} KB", maxFileSize / 1024);
                }
                else
                {
                    if (CheckFileType(upFile.FileName))
                    {
                        String filePath = "~/UploadImages" + upFile.FileName;
                        upFile.SaveAs(MapPath(filePath));                    

                        ScriptManager.RegisterStartupScript(this, this.GetType(), "OptStatus", "<script>alert('File Uploaded Successfully!');</script>", false);
                    }
                    else
                    {                       
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "OptStatus", "<script>alert('Invalid Image File!');</script>", false);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw;
        }       
    }

    // To check file type
    public bool CheckFileType(String fileName)
    {
        try
        {
            switch (Path.GetExtension(fileName).ToLower())
            {
                case ".gif":
                case ".png":
                case ".jpg":
                case ".jpeg":
                    return true;
                default:
                    return false;
            }
        }
        catch (Exception ex)
        {
            throw;
        }
    }
}
Download the source code click here
Please leave your comments, suggestions and queries about this post in the comment sections in order for me to improve my writing skills and to showcase more useful posts. Thanks for reading!
Verification and Validation Upload File Type and File Size - ASP .Net Reviewed by Ravi Kumar on 1:57 PM Rating: 5

No comments:

All Rights Reserved by Etechpulse © 2012 - 2017

Contact Form

Name

Email *

Message *

Powered by Blogger.