Grouping Download Lists into Categories

Mar 7, 2013 at 7:43 PM
I NEED HELP using dotnetzip and VB!

I need to separate the files to download and zip into four categories that are listed separately but the user will be able to select files from all categories for download. (I just want one download button instead of four).

Here's a link to my attempt to get this working: http://www.healthequity.com/NewSalesTest/checkbox/test4.aspx

For simplicity and since I'm a newbie, I'm trying to work out the kinks with just two categories. My code is below:

Dim width as String = "100%"
Public Sub Page_Load (ByVal sender As Object, ByVal e As System.EventArgs)
Try
    If Not ( Page.IsPostBack ) Then
        ' populate the dropdownlist
                    Dim hsaPath as  String= Server.MapPath("docs/hsa")
        Dim hraPath as  String= Server.MapPath("docs/hra")

                    Dim hsaFilenames As New List(Of String)(System.IO.Directory.GetFiles(hsaPath))
        Dim hraFilenames As New List(Of String)(System.IO.Directory.GetFiles(hraPath))


        Dim hsaDocs as List(Of String) = _
            hsaFilenames.ConvertAll (Function(s) s.Replace(hsaPath & "\", ""))

        Dim hraDocs as List(Of String) = _
            hraFilenames.ConvertAll (Function(s) s.Replace(hraPath & "\", ""))

        ErrorMessage.InnerHtml = ""

        hsaFileListView.DataSource = hsaDocs
        hraFileListView.DataSource = hraDocs
        hsaFileListView.DataBind()
        hraFileListView.DataBind()
    End If

Catch
    ' Ignored
End Try
End Sub



Public Sub btnGo_Click (ByVal sender As System.Object, ByVal e As System.EventArgs)
ErrorMessage.InnerHtml =""   ' debugging only

Dim HSAfilesToInclude as New System.Collections.Generic.List(Of String)()
Dim HRAfilesToInclude as New System.Collections.Generic.List(Of String)()

Dim hsaPath as String= Server.MapPath("docs/hsa")
Dim hraPath as String= Server.MapPath("docs/hra")

Dim HSAsource As DataKeyArray= hsaFileListView.DataKeys
Dim HRAsource As DataKeyArray= hraFileListView.DataKeys
For Each item As ListViewDataItem in hsaFileListView.Items
    Dim chkbox As CheckBox= CType(item.FindControl("HSAinclude"), CheckBox)
    Dim lbl As Label = CType(item.FindControl("HSAlabel"), Label)

    If Not (chkbox Is Nothing  OR  lbl Is Nothing) Then
        If (chkbox.Checked) Then
            ErrorMessage.InnerHtml = ErrorMessage.InnerHtml & _
                   String.Format("adding file: {0}<br/>", lbl.Text)
            HSAfilesToInclude.Add(System.IO.Path.Combine(hsaPath,lbl.Text))
            'filesToInclude.Concat(HSAfilesToInclude)
        End If
    End If
Next

For Each item As ListViewDataItem in hraFileListView.Items

   Dim chkbox As CheckBox= CType(item.FindControl("HRAinclude"), CheckBox)
   Dim lbl As Label = CType(item.FindControl("HRAlabel"), Label)

    If Not (chkbox Is Nothing  OR  lbl Is Nothing) Then
        If (chkbox.Checked) Then
            ErrorMessage.InnerHtml = ErrorMessage.InnerHtml & _
                    String.Format("adding file: {0}<br/>", lbl.Text)
            HRAfilesToInclude.Add(System.IO.Path.Combine(hraPath,lbl.Text))
            'filesToInclude.Concat(HRAfilesToInclude)
        End If
   End If
Next

Dim filesToInclude as New System.Collections.Generic.List(Of String)()
filesToInclude.Concat(HSAfilesToInclude)
filesToInclude.Concat(HRAfilesToInclude)

 If (filesToInclude.Count=0) Then
    ErrorMessage.InnerHtml = ErrorMessage.InnerHtml & "You did not select any files!"
Else
    Response.Clear
    Response.BufferOutput= false

    Dim c As System.Web.HttpContext = System.Web.HttpContext.Current
    Dim ReadmeText As String= String.Format("README.TXT\n\nHello!\n\n" & _
                                     "This is a zip file that was dynamically generated at {0}\n" & _
                                     "by an ASP.NET Page running on the machine named '{1}'.\n" & _
                                     "The server type is: {2}\n" & _
                                     System.DateTime.Now.ToString("G"), _
                                     System.Environment.MachineName, _
                                     c.Request.ServerVariables("SERVER_SOFTWARE"))
    Dim archiveName as String= String.Format("archive-{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"))
    Response.ContentType = "application/zip"
    Response.AddHeader("Content-Disposition", "inline; filename=" & chr(34) & archiveName & chr(34))

    Dim tempfile As String = "c:\temp\" & archiveName
    Using zip as new ZipFile()
        ' the Readme.txt file will not be password-protected.
        zip.AddEntry("Readme.txt", ReadmeText, Encoding.Default)

        ' filesToInclude is a string[] or List<String>
        zip.AddFiles(filesToInclude, "files")

        ' save the zip to a filesystem file
        zip.Save(tempfile)
    End Using

    ' open and read the file, and copy it to Response.OutputStream
    Using fs as System.IO.FileStream = System.IO.File.OpenRead("c:\temp\" & archiveName)
        dim b(1024) as Byte
        dim n as New Int32
        n=-1
        While (n <> 0)
            n = fs.Read(b,0,b.Length)
            If (n <> 0)
                Response.OutputStream.Write(b,0,n)
            End If
        End While
    End Using
    Response.Close
    System.IO.File.Delete(tempfile)

End If
End Sub


</script>



<html>
<head> <link rel="stylesheet" href="style/basic.css"> </head> <body>
<form id="Form" runat="server">

  <span class="SampleTitle"><b>Check the boxes to select the files, set a password if you like, then click the button to zip them up.</b></span>
  <br/>
  <br/>
  <span style="color:red" id="ErrorMessage" runat="server"/>
  <br/>
<h2>HSA</h2>
  <asp:ListView ID="hsaFileListView" runat="server">

    <LayoutTemplate>
      <table>
        <tr ID="itemPlaceholder" runat="server" />
      </table>
    </LayoutTemplate>

    <ItemTemplate>
      <tr>
        <td><asp:Checkbox ID="HSAinclude" runat="server"/></td>
        <td><asp:Label id="HSAlabel" runat="server" Text="<%# Container.DataItem %>" /></td>
      </tr>
    </ItemTemplate>

    <EmptyDataTemplate>
      <div>Nothing to see here...</div>
    </EmptyDataTemplate>

  </asp:ListView>
<h2>HRA</h2> 
  <asp:ListView ID="hraFileListView" runat="server">

    <LayoutTemplate>
      <table>
        <tr ID="itemPlaceholder" runat="server" />
      </table>
    </LayoutTemplate>

    <ItemTemplate>
      <tr>
        <td><asp:Checkbox ID="HRAinclude" runat="server"/></td>
        <td><asp:Label id="HRAlabel" runat="server" Text="<%# Container.DataItem %>" /></td>
      </tr>
    </ItemTemplate>

    <EmptyDataTemplate>
      <div>Nothing to see here...</div>
    </EmptyDataTemplate>

  </asp:ListView>

    <br/>
    <br/>
    <asp:Button id="btnGo" Text="Zip checked files" AutoPostBack OnClick="btnGo_Click" runat="server"/>

</form>
</body>

</html>