Wednesday 18 September 2013

How to Restrict the size of file upload in asp.net or not Exceed the the Predefined size when File upload by user in Asp.Net





Description:-

            In previous Example we explain that How to Extract Unzip Files to zip Folder in asp.net using C#. now in this example we explain How to Restrict the user to Upload File with Predefined length means not exceed the predefined size.

In which we Include one FileUpload Control in a webpage that Accept the File and save it in Folder when user is Upload the File but the Different is that the File is only Uploaded when the Size of File are Less than or Equal to the Predefined Size.

Here is a Code that Demostrate How to set Predefined size For File Upload in Web.Config File in asp.Net like :

<system.web>
<httpRuntime executionTimeout="9999" maxRequestLength="2097151"/>
</system.web>


Now we Explain the Above keyword that are used in Web.Config file as shown below

httpRuntime:-

that are used to for Configuring the Runtime Settings and can be Declared at the machine,site,application etc…

executionTimeout:-

that’s defines that the maximum number of second that a Request is allowed to Excute if the time is Excced the ExecutionTimeout then it will automatically shutdown the Application that is currentally running.

maxRequestLength:-

that’s defines that the maximum size of File Upload. This is the predefined size in kilobytes(KB). If Excced the predefined size then the Exception was Generated by the Application.the Default size of the File Upload is the 4096 KB(4 MB).


To Show Example of How to Upload File in MVC Application then click here upload Image and bind to Gridview in MVC

To show Example of How to Upload Multiple File in MVC Application then click here upload multiple File in MVC

How to upload File and Fetch file from SqlServer and bind to Gridview fetch file from Sqlserver and bind to Gridview


restrictFileSize.aspx:-




<%@ Page Language="C#" AutoEventWireup="true" CodeFile="restrictFileSize.aspx.cs" Inherits="restrictFileSize" %>

 <html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>



restrictFileSize.aspx.cs:-


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;
using System.Web.UI.WebControls;

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            if (FileUpload1.PostedFile.ContentLength < 20728650)
            {
                try
                {
                    Label1.Text = "File name: " +
                    FileUpload1.PostedFile.FileName + "<br>" +
                    FileUpload1.PostedFile.ContentLength + " kb<br>" +
                    "Content type: " +
                    FileUpload1.PostedFile.ContentType;
                }
                catch (Exception ex)
                {
                    Label1.Text = "ERROR: " + ex.Message.ToString();
                }
            }
            else
            {
                Label1.Text = "File size exceeds maximum limit 20 MB.";
            }
        }
    }

}
 

0 comments:

Post a Comment