Wednesday, 13 November 2013

Sample Project for downloading Files in ZIP Format

for .cs file we use this below code
----------------------------------

  protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // Calling the function of Grid View Bind
            BindGridview();
        }

    }
    protected void BindGridview()
    {
        // Creation of String arrary to store path
        string[] getfilesPath = Directory.GetFiles(Server.MapPath("~/FilesUploaded/"));
        // Adding files to list
        List<ListItem> files = new List<ListItem>();
        foreach (string path in getfilesPath)
        {
            //Adding files to the list item
            files.Add(new ListItem(Path.GetFileName(path)));
        }
        xfiles.DataSource = files;
        xfiles.DataBind();
    }


    protected void btnUpload_Click(object sender, EventArgs e)
    {
        // Uploading the Files to the folder
        if (xfuupload.HasFile)
        {
            string filename = Path.GetFileName(xfuupload.PostedFile.FileName);
            xfuupload.SaveAs(Server.MapPath("FilesUploaded/") + filename);
            xlit.Text = "File Uploaded Successfully";
            BindGridview();
        }
    }
    protected void btnDownload_Click(object sender, EventArgs e)
    {
        // Creating an Object of ZipFile Class in Iconic.Zip dll
        ZipFile creatingzip = new ZipFile();
        //Finding the Checkbox Control and checking which of the files are selected using the check box
        foreach (GridViewRow gvrow in xfiles.Rows)
        {
            CheckBox chk = (CheckBox)gvrow.FindControl("chkselectfordownload");
            if (chk.Checked)
            {
                string fileName = gvrow.Cells[1].Text;
                string filePath = Server.MapPath("~/FilesUploaded/" + fileName);
                // Adding the file to Zip Archieve
                creatingzip.AddFile(filePath, "files");
            }
        }
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment; filename=Downloads.zip");
        // Setting the content type to ZIP
        Response.ContentType = "application/zip";
        creatingzip.Save(Response.OutputStream);
        Response.End();
    }


for designing page
------------------
<form id="form1" runat="server">
     Sample Project for downloading Files in ZIP Format<p>
        <asp:FileUpload ID="xfuupload" runat="server" /> <br /><br />
        <asp:Button ID="btnUpload" runat="server" Text="Upload"
            onclick="btnUpload_Click" />
    </p>
    <p>
        <asp:Literal ID="xlit" runat="server"></asp:Literal>
    </p>

    <div>
     <asp:GridView ID="xfiles" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:TemplateField HeaderText="Select Files">
                    <ItemTemplate>
                        <asp:CheckBox ID="chkselectfordownload" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Text" HeaderText="Files" />
            </Columns>
        </asp:GridView>
        <br />
        <br />
        <asp:Button ID="btnDownload" runat="server" Text="Download Selected Files"
            onclick="btnDownload_Click" />

    </div>
    </form>



output like this





No comments:

Post a Comment