locked
Downloading Multiple Files as a Zip File Using GridView RRS feed

  • Question

  • User-1453200658 posted

    Hello there,

    This is my table on MySQL database.

    +----+----------------------------------+---------------------------------+----------------------------------+---------------------------------+
    | ID | xls                              | img                             | ppt                              | pdf                             |
    +----+----------------------------------+---------------------------------+----------------------------------+---------------------------------+
    |  1 | c:\inetpub\wwwroot\xls\test.xlsx | c:\inetpub\wwwroot\img\test.jpg | c:\inetpub\wwwroot\ppt\test.pptx | c:\inetpub\wwwroot\pdf\test.pdf |
    +----+----------------------------------+---------------------------------+----------------------------------+---------------------------------+
    

    I need publish the values of this table on gridview using asp net and c#

    Would it be possible to provide a single download button in the gridview and download a single zip file that joins the individual four files of row number one of the table?

    Thanks in advance for any help.

    Thursday, February 11, 2021 10:06 PM

Answers

  • User1535942433 posted

    Hi Edward Sheriff Curtis,

    As far as I think,you could install SharpZipLib . More details,you could refer to below this:

     <asp:GridView ID="griddownload" runat="server" AutoGenerateColumns="false" AlternatingRowStyle-BackColor="#99ccff" HeaderStyle-BackColor="#000099" HeaderStyle-ForeColor="White">
                    <Columns>
                        <asp:TemplateField HeaderText="check the check box to download the files">
                            <ItemTemplate>
                                <asp:CheckBox ID="chkselect" runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="File Name">
                            <ItemTemplate>
                                <asp:Label ID="lblFileName" runat="server" Text='<%# Eval("FileName") %>' />
                                <asp:Label ID="lblFilePath" Visible="false" runat="server" Text='<%# Eval("FilePath") %>' />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </div>
            <asp:Button runat="server" ID="button1" OnClick="button1_Click" />

    Code-behind:

     private void bindgrid()
            {
                var files = Directory.GetFiles(Server.MapPath("~/Zipped File"));
    
                griddownload.DataSource = from f in files
                                          select new
                                          {
                                              FileName = Path.GetFileName(f),
                                              FilePath = f
                                          };
                griddownload.DataBind();
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    bindgrid();
                }
            }
    
            protected void button1_Click(object sender, EventArgs e)
            {
                byte[] buffer = new byte[6144];
                var filedownload = Server.MapPath(@"tempfolder/" + Guid.NewGuid().ToString() + ".zip");
    
                var zipOutputStream = new ZipOutputStream(File.Create(filedownload));
                var filePath = String.Empty;
                var fileName = String.Empty;
                var readBytes = 0;
    
                foreach (GridViewRow row in griddownload.Rows)
                {
                    var isChecked = (row.FindControl("cboxselect") as CheckBox).Checked;
                    if (!isChecked) continue;
                    fileName = (row.FindControl("lblfname") as Label).Text;
                    filePath = (row.FindControl("lblpath") as Label).Text;
                    var addzipfile = new ZipEntry(fileName);
    
                    zipOutputStream.PutNextEntry(addzipfile);
    
                    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 = "First check minimum one check box then download!!!";
                    return;
                }
    
                zipOutputStream.Finish();
                zipOutputStream.Close();
                Response.ContentType = "application/x-zip-compressed";
                Response.AppendHeader("Content-Disposition", "attachment; filename=DownloadedFile.zip");
                Response.WriteFile(filedownload);
                Response.Flush();
                Response.Close();
                if (File.Exists(filedownload))
           }

    More details,you could refer to below article:

    https://www.c-sharpcorner.com/UploadFile/66489a/download-multiple-file-through-grid-view-in-a-single-zipped/

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 12, 2021 6:56 AM