前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >从数据库中下载文件的工具

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

作者头像
用户2434869
发布2018-09-12 10:40:51
1.2K0
发布2018-09-12 10:40:51
举报
文章被收录于专栏:yl 成长笔记yl 成长笔记

Obatain images

代码语言:javascript
复制
 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;
        }
    }


属性索引代码

代码语言:javascript
复制
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 结构

代码语言:javascript
复制
<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>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-05-25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档