connectseekers.com

Wednesday, 16 July 2014

Restrict User to Upload File of a Specific Size in Asp.Net Using C#.Net

7/16/2014 - By Pranav Singh 0

This article will show you how you can restrict user to upload file of a specific size in asp.net using c#.net on server side.


For this article first we will create a new asp.net application and add fileupload control and a button control.

  <div>
        <h2>
            <asp:Label ID="lblmessage" runat="server" Style="color: #FF0000; font-size: small;"
                Text=""></asp:Label>
        </h2>
        Upload File :<asp:FileUpload ID="FileUpload1" runat="server" />
        <br />
        (allowed only max 2MB to upload) <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Upload" />
    </div>

Now we will create the button click event by clicking on button control. Now use the below code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UploadFileinAsp.net
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                /*1MB=1024KB*/
                long filesize = ((FileUpload1.PostedFile.ContentLength) / 1024) / 1024;              
                if (filesize < 2)
                {
                    string filename = System.IO.Path.GetFileName(FileUpload1.FileName);
                    FileUpload1.SaveAs(Server.MapPath("~/document/" + filename));
                    lblmessage.Text = "File uploaded successfully.";
                }
                else
                {
                    lblmessage.Text = "You are allowed to upload more then 2MB.";
                }
            }
        }
    }
}

In above code we have get the uploaded file size by FileUpload1.PostedFile.ContentLength.  After getting file size we converted the byte into MB by dividing 1024. So we have done with code. Now run the page.

But before this we will make a configuration change. Add the below setting in web.config.

<system.web>
  <httpRuntime executionTimeout="240" maxRequestLength="20480" />
</system.web>


Have a look of the file size we are going to upload whose size is 4MB.


Now we will select the file click on browse button and click on upload .



 Now have a look of the file of size less than 2MB.


Now select file and click on upload.


Now click on upload after selecting file.


DOWNLOAD

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

0 comments:

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