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.
In my previous article I have shown you File
Upload with ASP.NET | How to Use FileUpload Control in ASP.Net Using
C#.Net,VB.Net | Upload File in Asp.net and Save in Folder, Ajax
FileUpload Control In Asp.Net or Multiple FileUpload With Progress Example in
Asp.Net Using C#.Net.
Some of my radiobuttonlist articles are as follows RadioButtonList
Validation Using jQuery in Asp.Net, Asp.Net-Validating
Radio Button List using Required Filed Validator | How to Use Required Field
Validator For Radiobuttonlist in Asp.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" />
<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.
How to check for 2 mb??
ReplyDeleteHi Dhruti,
Delete2MB 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.