从数据库中下载文件的工具

Obatain images

 public class Program
    {
        public static void Main(string[] args)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(@"E:\Project\ObtainImages\ObtainImages\ImageCategloryId.xml");
            //获取节点列表 
            //XmlNodeList topM = xmldoc.SelectNodes("//first");
            XmlNodeList root = xmldoc.SelectNodes("/root");
            XmlNodeList firstNodeList = root[0].ChildNodes;
            //int num = root.Count;
            foreach (XmlNode firNode in firstNodeList)
            {
                //string address = "D:/data";
                ReadNode(firNode);
            }
        }

        private static void ReadNode(XmlNode node, string saveAddress="D:/data")
        {
            string currentId = node.Attributes["id"].Value;
            string currentAddress = string.Format("{0}/{1}", saveAddress, currentId);
             List<MatQueue> matQueues = GetImageDataList(currentId);
            if (matQueues.Count() != 0)
            {
                DownloadImageByAddress(currentAddress, matQueues);
            }
            if (node.HasChildNodes)
            {
                XmlNodeList nextNodeList = node.ChildNodes;
                foreach (XmlNode nextNode in nextNodeList)
                {
                    ReadNode(nextNode,currentAddress);
                }
            }
        }
        

        private static void DownloadImageByAddress(string saveAddress, List<MatQueue> matQueues)
        {
            // 下载图片
            string root = "//172.18.4.2/assets2/";
            string uri_root = "http://local.image.test/imagedir";
            foreach (MatQueue item in matQueues)
            {
                string dataPath = item.imagepath;
                string address = root + item.imagepath;
                string dir = Path.GetDirectoryName(address);

                if (dataPath.Count()<20)
                {
                    return;
                }
                string end_uri = dataPath.Remove(0, 7);
                string uri = uri_root + end_uri;

                try
                {
                    if (Directory.Exists(dir))
                    {
                        System.Net.WebClient webClient = new System.Net.WebClient();
                        if (!Directory.Exists(saveAddress))
                        {
                            Directory.CreateDirectory(saveAddress);
                        }
                        webClient.DownloadFile(uri, saveAddress +"/"+ item.materialid + ".jpg");
                        Console.WriteLine(uri);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

        private static List<MatQueue> GetImageDataList(string categoryId)
        {
            string address = null;
            List<MatQueue> matQueue = new List<MatQueue>();

            using (OracleConnection cn = GetOraConnection())
            {
                //string sqlGetAddress = "Select caddataaddress From Cadqueue Where extdata = :MaterialId";
                string sqlGetAddress = "select  materialid, imagepath from pmc.designmaterial q where  q.CATEGORYID = :CategoryId  AND q.imagepath is not null";
                OracleCommand cmd = new OracleCommand(sqlGetAddress, cn);
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.Add(new OracleParameter("CategoryId", categoryId));
                cn.Open();

                OracleDataReader dtr = cmd.ExecuteReader();
                while (dtr.Read())
                {
                    matQueue.Add(PopulateQueuesFromIDataReader(dtr));
                }

                dtr.Close();
                cn.Close();
            }
            return matQueue;
        }

        public static OracleConnection GetOraConnection()
        {
            string constr = ConfigurationManager.AppSettings["DataBaseString"];
            return new OracleConnection(constr);
        }

        public static MatQueue PopulateQueuesFromIDataReader(System.Data.IDataReader dr)
        {
            MatQueue queues = new MatQueue();

            //循环读取各字段
            for (int i = 0; i < dr.FieldCount; i++)
            {
                if (dr[i] != System.DBNull.Value) // 利用属性索引
                {
                    queues[dr.GetName(i)] = dr[i];
                }
            }
            return queues;
        }
    }


属性索引代码

public class MatQueue
    {
        public string materialid;
        public string imagepath;

        //属性索引
        public object this[string propertyName]
        {
            get
            {
                #region get
                switch (propertyName.ToLower())
                {
                    case "materialid":
                        return this.materialid;
                    case "imagepath":
                        return this.imagepath;
                    default:
                        return null;
                }
                #endregion
            }
            set
            {
                #region set
                switch (propertyName.ToLower())
                {
                    case "materialid":
                        materialid = Convert.ToString(value);
                        break;
                    case "imagepath":
                        this.imagepath = Convert.ToString(value);
                        break;
                }
                #endregion
            }
        }

    }

Xml 结构

<root>
  <first id="erwqr">
    <second id="fsad">
      <third id="1"></third>
      <third id="2"></third>
      <third id="3"></third>
      <third id="4"></third>
    </second>
  </first>
</root>

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏菩提树下的杨过

(Head First 设计模式)学习笔记(1)

把书中开篇duck问题的代码翻译成了c#版: 概述: 某游戏公司要设计一款鸭子的游戏,里面有各种各样的鸭子,有些会飞,有些会叫,但都会游泳。而且不同种类的鸭子...

21390
来自专栏c#开发者

asp.net回调javascript

using System; using System.Collections; using System.IO; using System.Text; ...

33750
来自专栏跟着阿笨一起玩NET

linq to sql 三层架构中使用CRUD操作

16810
来自专栏互联网开发者交流社区

ORM框架(ITDOS实战源码)

24840
来自专栏c#开发者

让web api 4.5支持Jquery.getJson(url,handle)跨域访问

代码片段,google了半天,找到的都是4.5 rc,或之前版本的代码,发现都不能用,正式版后有些方法做了修改。我重新修改一下分享给大家 public stat...

28750
来自专栏码字搬砖

httpClient 发送压缩文件

41730
来自专栏王磊的博客

HttpWebRequest采集读取网站挂载Cookie的通用方法

Asp.net 版本 HttpWebRequest采集时添加:httpWebRequest.CookieContainer = new CookieContai...

32350
来自专栏菩提树下的杨过

几种常见复合sql查询语句的linq写法[继续补充中]

1.IN 语句 尝试了很多次,好象linq死活不生成in语句,无奈只能做了下变换 例,要找出"手机大类"以及"手机下属所有子类"的产品 (手机大类的ID为D0...

25960
来自专栏技术之路

动态生成TreeView方法(一)

一般情况下生成TreeView我们用的是用递归不建议用递归数据量大了会慢, 小弟今天用SortedList集合实现一下没有技术含量,一看代码大家就会明白。个人也...

19760
来自专栏跟着阿笨一起玩NET

C#自定义泛型类绑定ComboBox控件

C# WinForm ComboBox 自定义数据项 (ComboBoxItem )

27610

扫码关注云+社区

领取腾讯云代金券