Hi Ashcool,
Refer below modified code.
The FilePath function is modofied by passing another parameter as Candidate_Ref_No and set from GridView aswell.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" EmptyDataText="No files available">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" onclick="SelectAll(this)" Text="Select ALl" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" onclick="SelectOne(this)" />
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Candidate_Ref_No")%>' Visible="true"></asp:Label>
<asp:Image ImageUrl='<%# ConvertImage(Eval("Photodata"))%>' ID="ImageData" Width="50Px"
Height="50px" runat="server" />
<asp:Label ID="lblFilePath" runat="server" Text='<%# FilePath(Eval("Photodata"), Eval("Candidate_Ref_No"))%>' Visible="false" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnOnDownload" runat="server" Text="Download" OnClick="OnDownload" />
Code
Private constring As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
DeleteFile()
GetData()
End If
End Sub
Private Sub GetData()
Dim con As New SqlConnection(constring)
Dim cmd As New SqlCommand("SELECT Top 10 [Candidate_Ref_No]
,[Candidate_Name]
,[Photodata]
,[Signdata],id
FROM [5051].[dbo].[Candidate_PhotoSign_Details]", con)
Dim sda As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Protected Sub OnDownload(sender As Object, e As EventArgs)
Dim filePath As String = String.Empty
Using zip As New ZipFile()
zip.AlternateEncodingUsage = ZipOption.AsNecessary
zip.AddDirectoryByName("Files")
For Each row As GridViewRow In GridView1.Rows
If TryCast(row.FindControl("chk"), CheckBox).Checked Then
filePath = TryCast(row.FindControl("lblFilePath"), Label).Text
zip.AddFile(filePath, "Files")
End If
Next
Response.Clear()
Response.BufferOutput = False
Dim zipName As String = [String].Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"))
Response.ContentType = "application/zip"
Response.AddHeader("content-disposition", Convert.ToString("attachment; filename=") & zipName)
zip.Save(Response.OutputStream)
Response.End()
End Using
End Sub
Private Sub DeleteFile()
Dim di As System.IO.DirectoryInfo = New DirectoryInfo(Server.MapPath("~/Files/"))
For Each file As FileInfo In di.GetFiles()
file.Delete()
Next
End Sub
Public Function ConvertImage(binaryImage As Object) As String
Dim bytes As Byte() = DirectCast(binaryImage, Byte())
Dim base64String As String = Convert.ToBase64String(bytes, 0, bytes.Length)
Return Convert.ToString("data:image/png;base64,") & base64String
End Function
Public Function FilePath(fileData As Object, candidate_Ref_No As Object) As String
'For Each row As GridViewRow In GridView1.Rows
' If row.RowType = DataControlRowType.DataRow Then
' 'Hide the Row if CheckBox is not checked
' row.Visible = TryCast(row.FindControl("Candidate_Ref_No"), CheckBox).Checked
' End If
'Next
Dim bytes As Byte() = DirectCast(fileData, Byte())
Dim base64String As String = Convert.ToBase64String(bytes, 0, bytes.Length)
Dim ms As New MemoryStream(bytes, 0, bytes.Length)
ms.Write(bytes, 0, bytes.Length)
Dim image As System.Drawing.Image = System.Drawing.Image.FromStream(ms, True)
Dim newFile As String = candidate_Ref_No + ".jpeg"
Dim filePath__1 As String = Path.Combine(Server.MapPath("~/Files/"), newFile)
image.Save(filePath__1, ImageFormat.Jpeg)
Return filePath__1
End Function