This article will show you how you can get the uploaded file
size in asp.net using c#.net and vb.net. In this article I will I have used
asp.net, C#.net, Vb.Net, fileupload control and button control.
Some of my previous articles are as follows: Restrict User to Upload Only
doc or .docx Using Fileupload in Asp.Net C#.Net, Multiple File Upload With
Asp.Net MVC C# and HTML5 | How to upload files to ASP.NET MVC application,
File Upload and Displaying
Them as Thumbnails in DataList in MVC3 By Using Razor, C#.Net, File Upload in C#.Net Windows Application,
File Upload in MVC3 By Using Razor, Client Side Validation of File
Type or Extention Permited to Upload in Asp.Net Using Javascript, How To Get File Size Uploaded
By FileUpload Control Using C# In Asp.Net, 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, How to Get the Full Path of
FileUpload Control in Asp.Net, Restrict User to Upload File
of a Specific Size in Asp.Net Using C#.Net.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FileUploadControl.aspx.cs"
Inherits="ProjectDemo_Asp.et.FileUploadControl" %>
<!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>Upload
files and Get Uploaded File Size Using C#.Net,VB.Net in Asp.Net</title>
<style type="text/css">
p.MsoNormal
{
margin-top:
0in;
margin-right:
0in;
margin-bottom:
10.0pt;
margin-left:
0in;
line-height:
115%;
font-size:
11.0pt;
font-family:
"Calibri" ,
"sans-serif";
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
</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>
<p class="MsoNormal" style="text-decoration: underline">
</p>
</form>
</body>
</html>
|
Now generate the button click event and add the below code.
C#.Net
///
/// Button click event to get uploaded file size
///
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
decimal filesize = ((FileUpload1.PostedFile.ContentLength) / 1024) / 1024;
lblmessage.Text = "Uploaded File Size Is : " + filesize.ToString() + " MB";
}
else
{
lblmessage.Text = "Please select file.";
}
}
|
VB.Net
'''
''' Button click event to get uploaded file size
'''
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
If FileUpload1.HasFile Then
Dim filesize As Decimal = ((FileUpload1.PostedFile.ContentLength) / 1024) / 1024
lblmessage.Text = "Uploaded File Size Is : " + filesize.ToString() + " MB"
Else
lblmessage.Text = "Please select file."
End If
End Sub
|
But before this we will make a configuration change. Add the
below setting in web.config. This configuration we are adding to upload the file with large size.
<system.web>
<httpRuntime executionTimeout="240" maxRequestLength="20480" />
</system.web>
|
In above code whatever data we will get will be in byte so I
have converted it into MB by dividing it by 1024. So check the final output. So here is the uploaded file detail
Now select it and click on upload.
This blog post was so useful and informative. Thanks for sharing.
ReplyDeleteprimary school web design