Saturday, 30 November 2013

How To Upload Multiple File in your Application Folder in Asp.Net

How To Upload File in your Application Folder in Asp.Net

Here is the code to upload multiple file in your application folder.

Aspx Page:-


<div>

        <asp:FileUpload ID="fu_1" runat="server" multiple="true"></asp:FileUpload>

        <asp:Button ID="btn_upload" runat="server" Text="Upload" OnClick="btn_upload_Click" />

        <br />

        <asp:Label runat="server" ID="lblmsz" Font-Bold="true"></asp:Label>

    </div>


CS Page:-


Name Space Used:-

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


Upload Button Code:-


 protected void btn_upload_Click(object sender, EventArgs e)
        {
            int Total= 0,success= 0,unsuccess= 0;
            HttpFileCollection fileCollection = Request.Files;
            string notuploaded = string.Empty;
            for (int i = 0; i < fileCollection.Count; i++)
            {
                HttpPostedFile uploadfile = fileCollection[i];
                string fileName = Path.GetFileName(uploadfile.FileName);

                if (uploadfile.ContentLength < 5242880)
                {
                    if (CheckFileType(fileName))
                    {
                        string uploadpath = "~/Files/" + fileName;
                        fu_1.SaveAs(MapPath(uploadpath));
                        Total++;
                        success++;
                    }
                    else
                    {
                        Total++;
                        unsuccess++;
                    }
                }
                else
                {
                    Total++;
                    unsuccess++;
                }
            }
            lblmsz.Text = "<span style='color:Green;'>Total Files = " + Total + " Successful Uploads = " + success + "</span><span style='color:Red;'> Unsuccessful Uploads = " + unsuccess + "</span>";
         

        }


Check File Type Code:-


bool CheckFileType(string fileName)

        {

            string ext = Path.GetExtension(fileName);

            switch (ext.ToLower())

            {

                case ".gif":

                    return true;

                case ".png":

                    return true;
                case ".jpg":
                    return true;
                case ".jpeg":
                    return true;
                default:
                    return false;
            }
        }

Example:-


 1. Five files Selected 1 excel and 4 jpeg files

2.  Jpeg files are uploaded because Excel file allowed.


Don't forget to leave your feedback and comments below..!

Friday, 29 November 2013

How To Upload File in your Application Folder in Asp.Net

How To Upload File in your Application Folder in Asp.net


Here is the code to upload a file in your application folder.

Aspx Page:-

<div>
        <asp:FileUpload ID="fu_1" runat="server"></asp:FileUpload>
        <asp:Button ID="btn_upload" runat="server" Text="Upload" 
            onclick="btn_upload_Click" />
            <br />
            <asp:Label runat="server" ID="lblmsz" Font-Bold="true"></asp:Label>
    </div>

CS Page:-

Name Space Used:-

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


Upload Button Code:-


protected void btn_upload_Click(object sender, EventArgs e)
        {
            if (fu_1.HasFile)
            {
                if (CheckFileType(fu_1.FileName))
                {
                    string uploadpath = "~/Files/" + fu_1.FileName;
                    fu_1.SaveAs(MapPath(uploadpath));
                    lblmsz.Text = "File uploaded successfully";
                    lblmsz.ForeColor = Color.Green;
                }
                else
                {
                    lblmsz.Text = "Invalid file selected";
                    lblmsz.ForeColor = Color.Red;
                }
            }
            else
            {
                lblmsz.Text = "Please Select a file";
                lblmsz.ForeColor = Color.Red;
            }
        }


Check File Type Code:-


bool CheckFileType(string fileName)

        {

            string ext = Path.GetExtension(fileName);

            switch (ext.ToLower())

            {
                case ".gif":
                    return true;
                case ".png":
                    return true;
                case ".jpg":
                    return true;
                case ".jpeg":
                    return true;
                default:
                    return false;
            }
        }





Don't forget to leave your feedback and comments below..!

Thursday, 28 November 2013

SHA256 Encryption in Asp.Net

SHA256 Encryption in Asp.Net


Aspx Page:-


 <div>
        <table cellspacing="15px" width="21%" style="margin-left: 10%">
            <tr>
                <td colspan="2" align="center">
                    <asp:Label ID="Label13" runat="server" Style="text-align: center;" Text="Encrypt/Decrypt"
                        Font-Bold="true" Font-Size="28px"></asp:Label><br />
                    <br />
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label2" runat="server" AssociatedControlID="txt_str" Text="String To Encrypt"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txt_str" CssClass="TextBox" runat="server" AutoComplete="off" PlaceHolder="e.g :Sam"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="encrypt" runat="server" Text="Encrypt" OnClick="Encrypt_Click" />
                </td>
            </tr>
        </table>
        <asp:Label ID="lbl_enc" runat="server"></asp:Label>
    </div>



CS Page Code:-


Name Space Used:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Security;
using System.Security.Cryptography;

Button Code:-


protected void Encrypt_Click(object sender, EventArgs e)
        {
            string enc = MySHA256();
            lbl_enc.Text = enc;
        }


Method To Encrypt:-


      protected string MySHA256()
        {

            SHA256 sha256 = new System.Security.Cryptography.SHA256Managed();

            byte[] sha256Bytes = System.Text.Encoding.Default.GetBytes(txt_str.Text.Trim());

            byte[] cryString = sha256.ComputeHash(sha256Bytes);


            string sha256Str = string.Empty;


            for (int i = 0; i < cryString.Length; i++)
            {


                sha256Str += cryString[i].ToString("X");


            }


            return sha256Str;


        }






Don't forget to leave your feedback and comments below..!

Wednesday, 27 November 2013

SHA512 Encryption in Asp.Net

SHA512 Encryption in Asp.Net


Aspx Page:-


 <div>
        <table cellspacing="15px" width="21%" style="margin-left: 10%">
            <tr>
                <td colspan="2" align="center">
                    <asp:Label ID="Label13" runat="server" Style="text-align: center;" Text="Encrypt/Decrypt"
                        Font-Bold="true" Font-Size="28px"></asp:Label><br />
                    <br />
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label2" runat="server" AssociatedControlID="txt_str" Text="String To Encrypt"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txt_str" CssClass="TextBox" runat="server" AutoComplete="off" PlaceHolder="e.g :Sam"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="encrypt" runat="server" Text="Encrypt" OnClick="Encrypt_Click" />
                </td>
            </tr>
        </table>
        <asp:Label ID="lbl_enc" runat="server"></asp:Label>
    </div>


CS Page Code:-


Name Space Used:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Security;
using System.Security.Cryptography;

Button Code:-


protected void Encrypt_Click(object sender, EventArgs e)
        {
            string enc = MySHA512();
            lbl_enc.Text = enc;
        }


Method To Encrypt:-


        protected string MySHA512()
        {


            SHA512 sha512 = new System.Security.Cryptography.SHA512Managed();


            byte[] sha512Bytes = System.Text.Encoding.Default.GetBytes(txt_str.Text.Trim());


            byte[] cryString = sha512.ComputeHash(sha512Bytes);


            string sha512Str = string.Empty;


            for (int i = 0; i < cryString.Length; i++)
            {


                sha512Str += cryString[i].ToString("X");


            }

            return sha512Str.ToLower();

        }






Don't forget to leave your feedback and comments below..!

Tuesday, 26 November 2013

Simple Encryption Decryption in Asp.Net

Simple Encryption Decryption in Asp.Net

Note:-

Encrypted String Length will depend on the length of the string you have entered.

Aspx Page:-

<div>
        <table cellspacing="15px" width="21%" style="margin-left: 10%">
            <tr>
                <td colspan="2" align="center">
                    <asp:Label ID="Label13" runat="server" Style="text-align: center;" Text="Encrypt/Decrypt"
                        Font-Bold="true" Font-Size="28px"></asp:Label><br />
                    <br />
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label2" runat="server" AssociatedControlID="txt_str" Text="String To Encrypt/Decrypt"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txt_str" CssClass="TextBox" runat="server" AutoComplete="off" PlaceHolder="e.g :Sam"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="encrypt" runat="server" Text="Encrypt" OnClick="Encrypt_Click" />
                </td>
                <td>
                    <asp:Button ID="decrypt" runat="server" Text="Decrypt" OnClick="Decrypt_Click" />
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <asp:Label ID="lbl_dyc" runat="server"></asp:Label>
                </td>
            </tr>
        </table>
        <asp:Label ID="lbl_enc" runat="server"></asp:Label>
    </div>


CS Page Code:-

Name Space Used:-


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Security;
using System.Security.Cryptography;


Code:-

Encrypt Button:-

       protected void Encrypt_Click(object sender, EventArgs e)
        {
             string strpass = txt_str.Text;
            string dyc = Encrypt(strpass);
            lbl_dyc.Text = dyc;
        }

Decrypt Button:-

       protected void Decrypt_Click(object sender, EventArgs e)
        {
            string encryptedstr = txt_str.Text;
            string dyc = Decrypt(encryptedstr);
            lbl_dyc.Text = dyc;
        }

Method for Encrypt String :-

        public string Encrypt(string txtstring)
        {
            byte[] passBytes = System.Text.Encoding.Unicode.GetBytes(txtstring);
            string encrypt = Convert.ToBase64String(passBytes);
            return encrypt;
        }


Method for Decrypt String :-

        public string Decrypt(string encryptedstr)
        {
            byte[] passByteData = Convert.FromBase64String(encryptedstr);
            string originalstr = System.Text.Encoding.Unicode.GetString(passByteData);
            return originalstr;
        }

Encrytpion:-

Decrytpion:-




Don't forget to leave your feedback and comments below..!

How To Call A JQuery Popup From Asp.net Control

How To Call A JQuery Popup From Asp.net Control


<head>
<style type="text/css">
        body
        {
            background-color: #eee;
        }
        /* popup_box DIV-Styles*/
        #popup_box
        {
            display: none; /* Hide the DIV */
            position: fixed;
            _position: absolute; /* hack for internet explorer 6 */
            height: 300px;
            width: 600px;
            background: #FFFFFF;
            left: 300px;
            top: 150px;
            z-index: 100; /* Layering ( on-top of others), if you have lots of layers: I just maximized, you can change it yourself */
            margin-left: 15px; /* additional features, can be omitted */
            border: 2px solid #5EA5F2;
            padding: 15px;
            font-size: 15px;
            -moz-box-shadow: 0 0 5px #5EA5F2;
            -webkit-box-shadow: 0 0 5px #5EA5F2;
            box-shadow: 0 0 5px #5EA5F2;
        }
     
       .close
        {
            cursor: pointer;
            text-decoration: none;
        }
     
        /* This is for the positioning of the Close Link */
        #popupBoxClose
        {
            font-size: 20px;
            line-height: 15px;
            right: 5px;
            top: 5px;
            position: absolute;
            color: #6fa5e2;
            font-weight: 500;
        }
</style>
</head>

PopUp :- 


 <!-- OUR PopupBox DIV-->
<div id="popup_box">
        <h1>
            Sample JQuery Popup</h1>
        <h2>This is a simple popup box.</h2>
        <a id="popupBoxClose" class="close">Close</a>
    </div>
    <div>
 <asp:Button runat="server" ID="btn_show" Text="Show Popup" OnClientClick="return false;" />

Scripts:-


   <script src="http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js" type="text/javascript"></script>

    <script type="text/javascript">

        $(document).ready(function () {

            // When site loaded, load the Popupbox First
            //loadPopupBox();
            $('#btn_show').click(function () {
                loadPopupBox();
            });

            $('#popupBoxClose').click(function () {
                unloadPopupBox();
            });

            $('#container').click(function () {
                unloadPopupBox();
            });

            function unloadPopupBox() {    // TO Unload the Popupbox
                $('#popup_box').fadeOut(700);
                $("#container").css({ // this is just for style      
                    "opacity": "1"
                });
            }

            function loadPopupBox() {    // To Load the Popupbox
                $('#popup_box').fadeIn(700);
                $("#container").css({ // this is just for style
                    "opacity": "0.3"
                });
            }
        });
    </script>


Don't forget to leave your feedback and comments below..!

How to Compress JPEG File in Asp.Net

How to Compress JPEG File in Asp.Net:-

Through this code jpeg files will be compressed without loosing the resolution. 

First you need to create 2 folders in your project.
1. UploadImages
2. CompressedImages



Code For aspx page:-


<div>
<h3>Upload File To Compress</h3>
<asp:FileUpload ID="fu_Compress" runat="server" Style="background-color: #83CF32;
border-radius: 5px; -moz-border-radius: 5px; height: 30px; color: White; -webkit-border-radius: 5px;" />
<asp:RequiredFieldValidator ID="RFVfileupload" runat="server" ToolTip="Image required."
CssClass="failureNotification" Display="Dynamic" SetFocusOnError="true" ErrorMessage="Image required." ControlToValidate="fu_Compress" ValidationGroup="sbmitbtn" ForeColor="Red"></asp:RequiredFieldValidator>
<br />
(Max size limit 5MB per file)
<asp:Label Font-Bold="true" ID="lblmsz" runat="server"></asp:Label></p>
<asp:Button ID="btnsave" runat="server" Text="Compress" Style="background-color: #83CF32;
border-radius: 5px; -moz-border-radius: 5px; height: 30px; color: White; -webkit-border-radius: 5px;"
CssClass="Button" OnClick="btnsave_Click" ValidationGroup="sbmitbtn" />
</div>

Code For aspx.cs page:-


Name Spaces:-


using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Drawing;
using System.Web.Security;
using System.IO;
using System.IO.Compression;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Net;


Code:-


  private string ActualPath
{
            get { return (ViewState["ActualPath"] == null ? string.Empty : ViewState["ActualPath"].ToString()); }
            set { ViewState["ActualPath"] = value; }
        }

private string ImagePhotoPath
{
get { return (ViewState["Path"] == null ? string.Empty : ViewState["Path"].ToString()); }
set { ViewState["Path"] = value; }
}


Method for Compressing JPEG File:-


 private void CompressJpeg(double scaleFactor, Stream sourcePath, string targetPath, int width, int height, string fileName)
        {
            try
            {
                using (var image = Image.FromStream(sourcePath))
                {
                    var compImg = new Bitmap(width, height);
                    var newGraph = Graphics.FromImage(compImg);
                    newGraph.CompositingQuality = CompositingQuality.HighQuality;
                    newGraph.SmoothingMode = SmoothingMode.HighQuality;
                    newGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    var imageRectangle = new Rectangle(0, 0, width, height);
                    newGraph.DrawImage(image, imageRectangle);
                    compImg.Save(targetPath, image.RawFormat);
                    lblmsz.Text = "File Compressed Succesfully.";
                    lblmsz.ForeColor = Color.Green;
                   }
            }
            catch (Exception e)
            {
                lblmsz.Text = e.Message.ToString();
                lblmsz.ForeColor = Color.Red;
             }
        }



Code for Save Button:-


protected void btnsave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
           HttpFileCollection fileCollection = Request.Files;
                string notuploaded = string.Empty;
                string fileName = Path.GetFileName(fu_Compress.FileName);

                if (fu_Compress.PostedFile.ContentLength < 5242880)
                {

                    string ext4 = Path.GetExtension(fu_Compress.FileName).ToUpper();
                    string AllowedImageTypes = System.Configuration.ConfigurationManager.AppSettings["AllowedImageTypes"].ToString();
                    if (AllowedImageTypes.Contains(ext4))
                    {
                        string RelativePath = fu_Compress.FileName;
                        ActualPath = fu_Compress.FileName;
                        fu_Compress.SaveAs(Server.MapPath("~/UploadImages/" + fileName));
                        System.Drawing.Image objImage = System.Drawing.Image.FromFile(Server.MapPath("~/UploadImages/" + fileName));
                        int imgwid = objImage.Width;
                        int imghig = objImage.Height;
                        string targetPath = Server.MapPath("~/CompressedImages/" + fileName);
                        Stream strm = fu_Compress.PostedFile.InputStream;
                        var targetFile = targetPath;
                        CompressJpeg(0.5, strm, targetFile, imgwid, imghig, fileName);//Code use for without lossing image quality.

                        DateTime goodDateHolder = Convert.ToDateTime(DateTime.Now.ToShortDateString());

                    }
                    else
                    {
                        lblmsz.Text = "Only JPEG Files";
                        lblmsz.ForeColor = Color.Red;
                    }
                }
                else
                {
                    lblmsz.Text = "Max File Limit 5MB";
                    lblmsz.ForeColor = Color.Red;
                }

            }
        }







Don't forget to leave your feedback and comments below..!