Wednesday 8 October 2014

How to Restrict Size of Upload File or Image in Asp.Net C#

No comments    
categories: , , , ,
IMAGE SIZE RESTRIC


Image or File Upload  to Database  Restrict the File or Image Size  using ContentLenth in Asp.Net C#


                         DEMO





The maxRequestLength="1048576" (1MB)

Web.Config File Coding

 <httpRuntime  maxRequestLength="1048576"  />


Html Coding


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>

     <%-- Image Preview --%>

      <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">

    function imagepreview(input) {
        if (input.files && input.files[0]) {
            var filerdr = new FileReader();
            filerdr.onload = function (e) {
                $('#Image1').attr('src', e.target.result);
            }
            filerdr.readAsDataURL(input.files[0]);
        }
    }
    </script>
  <%--  Image PreView  --%>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      
        <asp:FileUpload ID="FileUpload1" runat="server" onchange="imagepreview(this)"/>
        <asp:Image ID="Image1" runat="server" Height="100px" ImageAlign="AbsMiddle" Width="100px" />         
        <br />
        <asp:Label ID="Label1" runat="server" ForeColor="#CC3300"></asp:Label>
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Save" />      
   
    </div>
    </form>
</body>
</html>

C# Coding

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;

public partial class img : System.Web.UI.Page
{

    SqlConnection con;
    SqlCommand cmd;
    SqlDataAdapter adp;
    SqlDataReader rd;
    DataSet ds;
    string query;
    string name;

    public void dbcon()
    {
        string connn = (System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
        con = new SqlConnection(connn);
        con.Open();

    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        dbcon();

        if ((FileUpload1.PostedFile != null) && (FileUpload1.PostedFile.ContentLength < 1048576))
        {

            name = Path.GetFileName(FileUpload1.PostedFile.FileName);

            FileUpload1.SaveAs(Server.MapPath("files/" + name));

            query = "insert into img(imagename,imagepath)values
                    ('" + name + "','" + "files/" + name + "')";

            cmd = new SqlCommand(query, con);
            cmd.ExecuteNonQuery();
            Response.Write("Image Upload");
        }
        else
        {
            Label1.Text = "File Size Accept  Upto 1MB.";
        }

    }

}















             

     






                                       i think useful this post.

0 comments:

Post a Comment