connectseekers.com

Friday, 22 August 2014

Upload files and Get Uploaded File Size Using C#.Net,VB.Net in Asp.Net

8/22/2014 - By Pranav Singh 1

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.

So for this article first we will create a new asp.net application and use the below mention html code in your .aspx page.

<%@ 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 ObjectByVal 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.


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

1 comment:

  1. This blog post was so useful and informative. Thanks for sharing.
    primary school web design

    ReplyDelete

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
Email Id : contact@aspdotnet-pools.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


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top