Download Images in Grid Rar Format


1.JPG

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

<!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>Student Deatils</title>
</head>
<
body>
   <form id="form1" runat="server">

     <ContentTemplate>
    <fieldset class="border">
    <table border="0" cellspacing="0" cellpadding="0" style="width: 90%">
                <tr>
                    <td width="16" class="one">
                        &nbsp;
                    </td>
                    <td>
                        <table width="80%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                                <td class="eight1" style="height: 18px; width: 37px;">
                                    &nbsp;
                                </td>
                                <td class="five" style="width: 122px;">
                                </td>
                                <td class="eight1" style="height: 18px">
                                    &nbsp;
                                </td>
                            </tr>
                        </table>
                    </td>
                    <td class="two">
                        &nbsp;
                    </td>
                </tr>
                <tr>
                    <td width="16" class="six">
                        &nbsp;
                    </td>
                    <td align="center">
                        <!-- content starts hear !-->
                        <table style="width: 100%">
                            <tr>
                                <td>
                                    <asp:Panel ID="pnlSearchwise" runat="server" SkinID="Panel">
                                        <table style="width: 100%">
                                        <tr>
                                        <td>
                                        <asp:Label ID="lblMessage" runat="server"></asp:Label>
                                        </td>
                                        </tr>
                                            <tr>
                                                <td>
                                                    <asp:RadioButtonList RepeatDirection="Vertical" ID="radioSearch" runat="server"
                                                      AutoPostBack="true"
                                                       
onselectedindexchanged="radioSearch_SelectedIndexChanged1">
                                                        <asp:ListItem>Age wise</asp:ListItem>
                                                        <asp:ListItem>Class wise</asp:ListItem>
                                                        <asp:ListItem>Student Name
wise</asp:ListItem>
                                                        <asp:ListItem>Date Wise</asp:ListItem>
                                                    </asp:RadioButtonList>
                                                </td>
                                            </tr>
                                        </table>
                                    </asp:Panel>
                                </td>
                                <td>
                                    <table style="width: 100%">

                                     <asp:Panel ID="pnlAge" runat="server" SkinID="Panel"
Visible="false">
                                            <tr>
                                                <td align="right">
                                                    <asp:Label ID="lblAge" runat="server" SkinID="Label" Text="Age:"></asp:Label>
                                                </td>
                                                <td align="left">
                                                    <asp:DropDownList ID="ddlAge" runat="server"
SkinID="DropDownList"
                                                        AutoPostBack="true" onselectedindexchanged="ddlAge_SelectedIndexChanged">
                                                    </asp:DropDownList>
                                                </td>
                                            </tr>
                                     </asp:Panel>
                                       <asp:Panel ID="pnlClass" runat="server" SkinID="Panel" Visible="false">
                                            <tr>
                                                <td style="text-align: right">
                                                    <asp:Label ID="lblClass" runat="server" SkinID="Label" Text="Class:"></asp:Label>
                                                </td>
                                                <td style="text-align: left">
                                                    <asp:DropDownList ID="ddlClass" runat="server" SkinID="DropDownList"
                                                       AutoPostBack="true" onselectedindexchanged="ddlClass_SelectedIndexChanged">
                                                    </asp:DropDownList>
                                                </td>
                                            </tr>
                                       </asp:Panel>
                                         <asp:Panel ID="pnlStudent" runat="server" SkinID="Panel"
Visible="false">
                                            <tr>
                                                <td style="text-align: right">
                                                    <asp:Label ID="lblStudent" runat="server" SkinID="Label" Text="Student Name:"></asp:Label>
                                                </td>
                                                <td style="text-align: left">
                                                    <asp:DropDownList ID="ddlStudent" runat="server" SkinID="DropDownList" AutoPostBack="true"
                                                       
onselectedindexchanged="ddlStudent_SelectedIndexChanged">
                                                    </asp:DropDownList>
                                                </td>
                                            </tr>
                                      </asp:Panel>
                                         <asp:Panel ID="pnldate" runat="server" SkinID="Panel"
Visible="false">
                                            <tr>
                                                <td style="text-align: right">
                                                    <asp:Label ID="lbldate" runat="server" SkinID="Label" Text="DateofCreation:"></asp:Label>
                                                </td>
                                                <td style="text-align: left">
                                                    <asp:DropDownList ID="ddldate" runat="server" SkinID="DropDownList" AutoPostBack="true"
                                                        onselectedindexchanged="ddldate_SelectedIndexChanged">
                                                    </asp:DropDownList>
                                                </td>
                                            </tr>
                                        </asp:Panel>
                                   </table>

                                 </td>
                            </tr>
                        </table>

                       </td>
                    <td width="16" class="seven">
                        &nbsp;
                    </td>
                </tr>
                <tr>
                    <td width="16" class="three">
                        &nbsp;
                    </td>
                    <td class="eight">
                        &nbsp;
                    </td>
                    <td width="16" class="four">
                        &nbsp;
                    </td>
                </tr>
            </table>
            <table style="width:90%">
             <tr>
              <td style="text-align:right">
                  &nbsp;</td>
             </tr>
            </table>
            </fieldset>
    </ContentTemplate>

    <table width="100%">
        <tr>
            <td align="center">
                <asp:GridView ID="GvStudentDetails" runat="server" SkinID="GridViewDesign"
                    AutoGenerateColumns="false" Width="50%"
                    onrowcommand="GvStudentDetails_RowCommand"
                    onpageindexchanging="GvStudentDetails_PageIndexChanging" PageIndex="5"
                    AllowPaging="true" PageSize="6"
                    >
               <Columns>
               <asp:TemplateField HeaderText="Sid">
                   <ItemTemplate >
                       <asp:Label ID="lblstuid" runat="server" Text='<%# bind("varStudentCode") %>'></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="SName">
                   <ItemTemplate >
                       <asp:Label ID="lblstuname" runat="server" Text='<%# BInd("varFirstName") %>'></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="StuImageCode">
                   <ItemTemplate >
                       <asp:Label ID="lblimagecode" runat="server"
                           Text='<%# BInd("varGeneratedCode") %>'></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="Filepath">
                   <ItemTemplate >
                     <%--<asp:Label ID="lblFileName" runat="server" Text='<%# Eval("FileName") %>' />--%>
                       <asp:Image ID="Image1" runat="server" ImageUrl='<%# Bind("FilePath") %>' Height="50px" Width="50px"  />
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField >
                   <HeaderTemplate>
                       <asp:CheckBox ID="chkimg" runat="server" oncheckedchanged="chkimg_CheckedChanged" AutoPostBack="true"/>
                   </HeaderTemplate>
                   <ItemTemplate>
                       <asp:CheckBox ID="chkitem" runat="server" />
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField >
               <ItemTemplate>
                   <asp:LinkButton ID="LinkButton1" runat="server" Text="Download"
CommandName="Download" Visible="false" CommandArgument='<%# Bind("FilePath") %>' ></asp:LinkButton>
               </ItemTemplate>
               </asp:TemplateField>
               </Columns>    

                </asp:GridView>

    </td>
    <td>

    </td>
    <td>
        <asp:Button ID="Button1" runat="server" Text="DownloadAll"
            onclick="Button1_Click1" style="height: 26px"
           />
    </td>
    </tr>
    </table>

    </form>
</body>
</
html>

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;

public partial class Scarch_Values : System.Web.UI.Page
{
    static OleDbDataAdapter da = new OleDbDataAdapter();
    static OleDbCommand cmd = new OleDbCommand();
    static DataSet ds = new DataSet();
    static string chkimg;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadAge();
            LoadClass();
            LoadStudentName();
            LoadDates();

         }

    }

    private void LoadDates()
    {
        string path = Server.MapPath("~/Database/Test.mdb");
        string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";";
        da = new OleDbDataAdapter("Select dateOfCreation From tabUploadImage", strConn);
        ds = new DataSet();
        ds.Reset();
        da.Fill(ds, "tabUploadImage");
        if (ds.Tables[0].Rows.Count > 0)
        {
            ddldate.DataSource = ds;
            ddldate.DataTextField = "dateOfCreation";
            ddldate.DataBind();
        }
        ddldate.Items.Insert(0, "Select");
    }

    private void LoadStudentName()
    {
        string path = Server.MapPath("~/Database/Test.mdb");
        string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";";
        da = new OleDbDataAdapter("Select varFirstName From tabStudent", strConn);
        ds = new DataSet();
        ds.Reset();
        da.Fill(ds, "tabStudent");
        if (ds.Tables[0].Rows.Count > 0)
        {
            ddlStudent.DataSource = ds;
            ddlStudent.DataTextField = "varFirstName";
            ddlStudent.DataBind();
        }
        ddlStudent.Items.Insert(0, "Select");
    }

    private void LoadClass()
    {
        string path = Server.MapPath("~/Database/Test.mdb");
        string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";";
        da = new OleDbDataAdapter("Select varClass From tabStudent", strConn);
        ds = new DataSet();
        ds.Reset();
        da.Fill(ds, "tabStudent");
        if (ds.Tables[0].Rows.Count > 0)
        {
            ddlClass.DataSource = ds;
            ddlClass.DataTextField = "varClass";
            ddlClass.DataBind();
        }
        ddlClass.Items.Insert(0, "Select");
    }

    private void LoadAge()
    {
        string path = Server.MapPath("~/Database/Test.mdb");
        string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";";
        da = new OleDbDataAdapter("Select varAge From tabStudent", strConn);
        ds = new DataSet();
        ds.Reset();
        da.Fill(ds, "tabStudent");
        if (ds.Tables[0].Rows.Count > 0)
        {
            ddlAge.DataSource = ds;
            ddlAge.DataTextField = "varAge";
            ddlAge.DataBind();
        }
        ddlAge.Items.Insert(0, "Select");
    }
    protected void radioSearch_SelectedIndexChanged1(object sender, EventArgs e)
    {
        if (radioSearch.SelectedValue == "Age wise")
        {
            pnlAge.Visible = true;
            pnlClass.Visible = false;
            pnlStudent.Visible = false;
            pnldate.Visible = false;
        }
        else if (radioSearch.SelectedValue == "Class wise")
        {
            pnlAge.Visible = false;
            pnlClass.Visible = true;
            pnlStudent.Visible = false;
            pnldate.Visible = false;
        }
        else if (radioSearch.SelectedValue == "Student Name wise")
        {
            pnlAge.Visible = false;
            pnlClass.Visible = false;
            pnlStudent.Visible = true;
            pnldate.Visible = false;
        }
        else if (radioSearch.SelectedValue == "Date Wise")
        {
            pnlAge.Visible = false;
            pnlClass.Visible = false;
            pnlStudent.Visible = false;
            pnldate.Visible = true;
        }
    }

    protected void ddlAge_SelectedIndexChanged(object sender, EventArgs e)
    {
        BIndAgeDetails();
    }

    private void BIndAgeDetails()
    {
        string path = Server.MapPath("~/Database/Test.mdb");
        string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";";
        da = new OleDbDataAdapter("SELECT tabStudent.varStudentCode, tabStudent.varFirstName, tabStudent.varGeneratedCode, tabUploadImage.FilePath FROM tabStudent INNER JOIN tabUploadImage ON tabStudent.varStudentCode = tabUploadImage.varStudentCode Where varAge='" + ddlAge.SelectedItem.Text + "'", strConn);
        ds = new DataSet();
        ds.Reset();
        da.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            GvStudentDetails.DataSource = ds;
            GvStudentDetails.DataBind();

        }
    }
    protected void ddlClass_SelectedIndexChanged(object sender, EventArgs e)
    {
        string path = Server.MapPath("~/Database/Test.mdb");
        string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";";
        da = new OleDbDataAdapter("SELECT tabStudent.varStudentCode, tabStudent.varFirstName, tabStudent.varGeneratedCode, tabUploadImage.FilePath FROM tabStudent INNER JOIN tabUploadImage ON tabStudent.varStudentCode = tabUploadImage.varStudentCode Where varClass='" + ddlClass.SelectedItem.Text + "'", strConn);
        ds = new DataSet();
        ds.Reset();
        da.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            GvStudentDetails.DataSource = ds;
            GvStudentDetails.DataBind();

        }
    }
    protected void ddlStudent_SelectedIndexChanged(object sender, EventArgs e)
    {
        string path = Server.MapPath("~/Database/Test.mdb");
        string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";";
        da = new OleDbDataAdapter("SELECT tabStudent.varStudentCode, tabStudent.varFirstName, tabStudent.varGeneratedCode, tabUploadImage.FilePath FROM tabStudent INNER JOIN tabUploadImage ON tabStudent.varStudentCode = tabUploadImage.varStudentCode Where varFirstName='" + ddlStudent.SelectedItem.Text + "'", strConn);
        ds = new DataSet();
        ds.Reset();
        da.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            GvStudentDetails.DataSource = ds;
           GvStudentDetails.DataBind();

         }
    }
    protected void ddldate_SelectedIndexChanged(object sender, EventArgs e)
    {
        string path = Server.MapPath("~/Database/Test.mdb");
        string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";";
        da = new OleDbDataAdapter("SELECT tabStudent.varStudentCode, tabStudent.varFirstName, tabStudent.varGeneratedCode, tabUploadImage.FilePath FROM tabStudent INNER JOIN tabUploadImage ON tabStudent.varStudentCode = tabUploadImage.varStudentCode Where dateOfCreation='" + ddldate.SelectedItem.Text + "'", strConn);
        ds = new DataSet();
        ds.Reset();
        da.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            GvStudentDetails.DataSource = ds;
            GvStudentDetails.DataBind();

         }
    }
    protected void GvStudentDetails_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //string path = Server.MapPath("~/Database/Test.mdb");
        //string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";";
        //da = new OleDbDataAdapter("SELECT tabStudent.varStudentCode, tabStudent.varFirstName, tabStudent.varGeneratedCode, tabUploadImage.FilePath FROM tabStudent INNER JOIN tabUploadImage ON tabStudent.varStudentCode = tabUploadImage.varStudentCode", strConn);
        //DataTable dt = new DataTable();
        //dt.Reset();
        //da.Fill(dt);
        if (e.CommandName == "Download")
        {

            foreach (GridViewRow gr in GvStudentDetails.Rows)
            {
                CheckBox chk = (CheckBox)gr.FindControl("chkitem");

                LinkButton lb = (LinkButton)gr.FindControl("LinkButton1");
                if (chk.Checked)
                {
                    string img = lb.CommandArgument.ToString();
                    string finalimagename = img.Substring(img.LastIndexOf('/') + 1);

                    string ss2 = "~/UploadImages/" + finalimagename;
                    string ss = Server.MapPath(ss2);
                    DownLoadAllImages(ss,finalimagename,e,sender);

                }
            }

            //foreach (GridViewRow gr in GvStudentDetails.Rows)
            //{
            //    CheckBox chk = (CheckBox)gr.FindControl("chkitem");

             //    LinkButton lb = (LinkButton)gr.FindControl("LinkButton1");
            //    if (chk.Checked)
            //    {
            //        string img = lb.CommandArgument.ToString();
            //        string finalimagename = img.Substring(img.LastIndexOf('/') + 1);
 
                   
            //    }
            //}
            //downloadtoOtherFolder();
            //string img = e.CommandArgument.ToString();

            //string finalimagename = img.Substring(img.LastIndexOf('/')+1);

            //System.IO.File.Copy(Server.MapPath("~/UploadImages/Sunset.jpg"), Server.MapPath("~/Images123/Sunset.jpg"));
        }
    }
    public void DownLoadAllImages(string ss, string finalimagename,GridViewCommandEventArgs e,object sender)
    {
        try
        {
            Response.ClearContent();
            Response.ClearHeaders();
            Response.ContentType = "image/jpeg";
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + finalimagename);
            Response.TransmitFile(ss);
        }
        catch (Exception ex)
        {

        }
        //GvStudentDetails_RowCommand(sender,e);
        //Response.End();
    }
    public void downloadtoOtherFolder()
    {
        try
        {
            foreach (GridViewRow gr in GvStudentDetails.Rows)
            {
                CheckBox chk = (CheckBox)gr.FindControl("chkitem");

                LinkButton lb = (LinkButton)gr.FindControl("LinkButton1");
                if (chk.Checked)
                {

                    string img = lb.CommandArgument.ToString();

                    string finalimagename = img.Substring(img.LastIndexOf('/') + 1);

                    string img2 = "~/UploadImages/" + finalimagename;
                    string img3 = "~/Images123/" + finalimagename;

                    string source = Server.MapPath(img2);
                    string destination = Server.MapPath(img3);

                    System.IO.File.Copy(source, destination);
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
    protected void chkimg_CheckedChanged(object sender, EventArgs e)
    {
        try
        {
            CheckBox chk;
            foreach (GridViewRow rowItem in GvStudentDetails.Rows)
            {
                chk = (CheckBox)(rowItem.Cells[0].FindControl("chkitem"));
                chk.Checked = ((CheckBox)sender).Checked;
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
   }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        ZipAllFiles(); ;
    }

    private void ZipAllFiles()
    {
        byte[] buffer = new byte[4096];

        var tempFileName = Server.MapPath(@"TempFiles/" + Guid.NewGuid().ToString() + ".zip");

        var zipOutputStream = new ZipOutputStream(File.Create(tempFileName));
        var filePath = String.Empty;
        var fileName = String.Empty;
        var readBytes = 0;
        foreach (GridViewRow gr1 in GvStudentDetails.Rows)
        {
            CheckBox chk = (CheckBox)gr1.FindControl("chkitem");

            LinkButton lb = (LinkButton)gr1.FindControl("LinkButton1");
            string img = lb.CommandArgument.ToString();

            string finalimagename = img.Substring(img.LastIndexOf('/') + 1);
            var isChecked = (gr1.FindControl("chkitem") as CheckBox).Checked;
            if (!isChecked) continue;

            filePath = Server.MapPath(img);

            fileName = finalimagename;

            var zipEntry = new ZipEntry(fileName);

            zipOutputStream.PutNextEntry(zipEntry);

            using (var fs = File.OpenRead(filePath))
            {
                do
                {
                    readBytes = fs.Read(buffer, 0, buffer.Length);
                    zipOutputStream.Write(buffer, 0, readBytes);

                }
                while (readBytes > 0);
            }

            if (zipOutputStream.Length == 0)
            {
                lblMessage.Text = "Please select at least one file!";
                return;
            }

            }
            zipOutputStream.Finish();
            zipOutputStream.Close();

            Response.ContentType = "application/x-zip-compressed";|
            Response.AppendHeader("Content-Disposition", "attachment; filename=AllImages.zip");
            Response.WriteFile(tempFileName);

            Response.Flush();
            Response.Close();

            // delete the temp file
            if (File.Exists(tempFileName))
                File.Delete(tempFileName);
        }

    protected void GvStudentDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GvStudentDetails.PageIndex = e.NewPageIndex;
        BIndAgeDetails();
    }
}

Up Next
    Ebook Download
    View all
    Learn
    View all