Wednesday, 11 June 2014

How To Get File Size Uploaded By FileUpload Control Using C# In Asp.Net

6/11/2014 - By Pranav Singh 2

When we upload a file on that case we always need to know what the size of the uploaded file. So in this article I will show you how you can get the uploaded file size in an asp.net application using C#.Net.



Now for this article we will create a new asp.net application and add the below code in your page

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GetUpoadedFileSize.aspx.cs" Inherits="ProjectDemo_Asp.et.GetUpoadedFileSize" %>

<!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>How To Get File Size Uploaded By FileUpload Control Using C# In Asp.Net | 
check file size before upload - C# / C Sharp
</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:FileUpload ID="FileUpload1" runat="server" />
            &nbsp;<asp:Button ID="Button1" runat="server" Text="Upload file"
                onclick="Button1_Click" />
            <br />
            <br />
            <asp:Label ID="lblmessage" runat="server" Style="color: #FFFFFF; font-weight: 700;
                background-color: #FF6699" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>

Now let’s come to our code end add the below

C#.Net
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
             long fileSize = FileUpload1.FileContent.Length;
             lblmessage.Text = "Selected File Size :" + fileSize.ToString();               
            }
            else
            {
                lblmessage.Text = "Please select file.";
            }
        }
    }
}

VB.Net
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace ProjectDemo_Asp.et
    Partial Public Class GetUpoadedFileSize
        Inherits System.Web.UI.Page
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

        End Sub

        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
            If FileUpload1.HasFile Then
                Dim fileSize As Long = FileUpload1.FileContent.Length
                lblmessage.Text = "Selected File Size :" + fileSize.ToString()
            Else
                lblmessage.Text = "Please select file."
            End If
        End Sub
    End Class
End Namespace

In above code I have fine the size of the file on button click event. Above code will return output in bytes. If you wants to return the code into KB just divide it with 1024.

Now put the break point and run the application.


Now select File



Now click on button. As you click your break point will hit and you will get the output as shown below.


Final output.




About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

2 comments:

  1. Replies
    1. Hi Dhruti,

      2MB will be convert MB into kb .

      1024*2=2048 KB

      and now we will convert KB into bytes
      2048*2=4096 bytes


      So Your 2MB will be equal to 4096


      NOTE: In above code if you device the file size by 1024 then you will get exact value exactly same as in screen shot tooltip.

      Delete

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top