Hi mukesh1,
You can't download each image file separately. You need to download all the images as Zip file.
Refer below sample.
You will need to download the DotNetZip Library DLL using the following command.
Install-Package Ionic.Zip -Version 1.9.1.8
HTML
<asp:Button Text="Download" runat="server" OnClick="OnDownload" />
Code
C#
using System.Data;
using System.IO;
using Ionic.Zip;
public partial class CS : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void OnDownload(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("ActivityFile");
dt.Rows.Add("1", "Desert.jpg");
dt.Rows.Add("2", "Hydrangeas.jpg");
dt.Rows.Add("3", "Jellyfish.jpg");
DataTable dtImages = dt;
string folderPath = Server.MapPath("~/Files");
using (ZipFile zip = new ZipFile())
{
zip.AlternateEncodingUsage = ZipOption.AsNecessary;
zip.AddDirectoryByName("Files");
foreach (DataRow row in dtImages.Rows)
{
string fileName = Path.GetFileName(row["ActivityFile"].ToString());
zip.AddFile(Path.Combine(folderPath, fileName), "Files");
}
Response.Clear();
Response.BufferOutput = false;
string zipName = string.Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment; filename=" + zipName);
zip.Save(Response.OutputStream);
Response.End();
}
}
}
VB.Net
Imports System.Data
Imports System.IO
Imports Ionic.Zip
Partial Class VB
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
End Sub
Protected Sub OnDownload(sender As Object, e As EventArgs)
Dim dt As DataTable = New DataTable()
dt.Columns.Add("id")
dt.Columns.Add("ActivityFile")
dt.Rows.Add("1", "Desert.jpg")
dt.Rows.Add("2", "Hydrangeas.jpg")
dt.Rows.Add("3", "Jellyfish.jpg")
Dim dtImages As DataTable = dt
Dim folderPath As String = Server.MapPath("~/Files")
Using zip As ZipFile = New ZipFile()
zip.AlternateEncodingUsage = ZipOption.AsNecessary
zip.AddDirectoryByName("Files")
For Each row As DataRow In dtImages.Rows
Dim fileName As String = Path.GetFileName(row("ActivityFile").ToString())
zip.AddFile(Path.Combine(folderPath, fileName), "Files")
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", "attachment; filename=" & zipName)
zip.Save(Response.OutputStream)
Response.End()
End Using
End Sub
End Class