简体   繁体   中英

Download Multiple Images which are stored as "image" datatype from Sql Server to a FOLDER using c# asp.net 4.5

I have images stored in my database, there are about 500 of them. I have stored them as an "image" data type. There is also eid field in each row which is unique. I need to download ALL these images into a SINGLE folder. The name of the image should be the EID of the row.

I am using C# & asp.net 4.5 with SQL Server 2012.

Can you please advise?

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.IO;
    using System.Data.SqlClient;
    using System.Net;
    using Ionic.Zip;

namespace test.admin
{
    public partial class downloadImages : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void submit_Click(object sender, EventArgs e)
        {
            // delete existing images in the folder
            var di = new DirectoryInfo(Server.MapPath("~/images"));
            foreach (var file in di.EnumerateFiles())
            {
                file.Delete();
            }

            // create images and store them in the images folder
            DataTable dt = GetData("SELECT * FROM mytable");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                byte[] bytes = (byte[])dt.Rows[i]["photo"];
                File.WriteAllBytes(Server.MapPath("~/images/" + dt.Rows[i]["eID"] + ".jpg"), bytes);
            }

            // use this to download the zip file
            using (ZipFile zip = new ZipFile())
            {
                zip.AlternateEncodingUsage = ZipOption.AsNecessary;
                zip.AddDirectory(Server.MapPath("~/images/"))

                Response.Clear();
                Response.BufferOutput = false;
                string zipName = String.Format("sasImages.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();
            }


        }

        protected void delete_Click(object sender, EventArgs e)
        {

            // delete existing images in the folder
            var di = new DirectoryInfo(Server.MapPath("~/images"));
            foreach (var file in di.EnumerateFiles())
            {
                file.Delete();
            }

            string script = "alert('All files in the folder deleted successfully');";
            ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script, true);
        }

        private DataTable GetData(string query)
        {
            DataTable dt = new DataTable();
            string constr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    using (SqlDataAdapter sda = new SqlDataAdapter())
                    {
                        cmd.CommandType = CommandType.Text;
                        cmd.Connection = con;
                        sda.SelectCommand = cmd;
                        sda.Fill(dt);
                    }
                }

                return dt;
            }
        }

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM