C# 寫的通用得到富文本框中的圖片路徑


富文本框(即 ueditor)是個好東西,功能很強大!開發者若需要去了解學習的話,可以訪問:http://ueditor.baidu.com/website/ 進行學習。

但是有得后端開發者需要取出其中某一類的標簽,比如需要取出里面的所有圖片等....

好,廢話不多說,直接上代碼:

例子:

     /// <summary>
        /// 通用得到富文本框中的圖片
        /// </summary>
        /// <returns></returns>
        private List<string> GetContentImg(string content)
        {
            //存儲圖片的集合
            List<string> imgList = new List<string>();
            //定義匹配變量
            int j = 0;
            //定義截取開始下標
            int startIndex = 0;
            //定義截取長度變量
            int len = 0;
            //從content中取出圖片
            for (int i = 0; i < content.Length; i++)
            {
                char ch = content[i];
                if (len > 0)
                {
                    len++;
                }
                //判斷是否滿足條件
                if ((j==0 && ch == 'i') || (j == 1 && ch == 'm') || (j == 2 && ch == 'g') || (j == 3 && ch == ' ') || (j == 5 && ch == 'r') ||(j == 6 && ch == 'c') ||(j == 7 && ch == '"')||(j == 4 && ch=='s')||(j == 8 && ch == '"'))
                {
                    if (j==4)
                    {
                        startIndex = i;
                        len++;
                    }
                    if (j == 8)
                    {
                        //開始截取
                        string str = content.Substring(startIndex, len);
                        imgList.Add(str);
                        //清空
                        startIndex = 0;
                        j = 0;
                        len = 0;
                    }
                    j++;
                    continue;
                }
            }
            var list = new List<string>();
            if (imgList.Count>0)
            {
                //得到服務器地址
                var cmsDomain = _configuration.GetSection("System:CMSDomainName").Value;
                foreach (var item in imgList)
                {
                    string url = item;
                    //拆分
                    var newUrl = url.Split('\"');
                    if (newUrl.Length>1)
                    {
                        url = newUrl[1].Replace("@/", "/");
                    }
                    //拼接
                    url = cmsDomain + url;
                    //驗證圖片是否可以在網絡上訪問
                    bool flag = FileClassify.JudgeFileExist(url);
                    if (flag)
                    {
                        list.Add(url);
                    }
                }
            }
            return list;
        }

其中:fileClassify是一個靜態的類

public static bool JudgeFileExist(string url)
        {
            try
            {
                //創建根據網絡地址的請求對象
                var httpWebRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
                httpWebRequest.Method = "HEAD";
                httpWebRequest.Timeout = 1000;
                //返回響應狀態是否是成功比較的布爾值
                var data= (HttpWebResponse)httpWebRequest.GetResponse();
                return data.StatusCode == HttpStatusCode.OK;
            }
            catch
            {
                return false;
            }
        }

呵呵,才寫好的。可能封裝性不是很好,也有可能會有漏洞,大家可以用 try catch 來捕獲異常,因為我也是初級程序員,后期還會繼續優化的!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM