簡體   English   中英

使用 c# asp.net 4.5 將存儲為“圖像”數據類型的多個圖像從 Sql Server 下載到文件夾

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

我的數據庫中存儲了圖像,其中大約有 500 個。 我將它們存儲為“圖像”數據類型。 每行還有一個唯一的eid字段。 我需要將所有這些圖像下載到一個文件夾中。 圖像的名稱應該是行的 EID。

我在 SQL Server 2012 中使用 C# 和 asp.net 4.5。

你能給些建議么?

    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;
            }
        }

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM