예제 #1
0
        public static ContentData SavePic(string HtmlContent, bool AutoMark)
        {
            ContentData cData = new ContentData();
            cData.Content = string.Empty;
            cData.FileList = new List<string>();

            string PicWaterMarkFile = ConfigurationManager.AppSettings["PicWaterMarkFile"];
            string FileSavePath = ConfigurationManager.AppSettings["FileSavePath"];
            string PicSaveWebPath = ConfigurationManager.AppSettings["PicSaveWebPath"];
            string PicUrl = ConfigurationManager.AppSettings["PicUrl"];

            string FileSubPath = string.Format("{0}/{1}/{2}/", DateTime.Now.ToString("yyyy"), DateTime.Now.ToString("MM"), DateTime.Now.ToString("dd"));
            PicSaveWebPath += FileSubPath;
            string SaveFullPath = FileSavePath + PicSaveWebPath.Replace("/", "\\");

            WebClient client = new WebClient();
            MatchCollection matchs = new Regex(@"((http|https|ftp|rtsp|mms):(\/\/|\\\\){1}([\w\-]+[.]){1,}(net|com|cn|org|cc|tv|[0-9]{1,3})(([^.])+[.]{1}(gif|jpg|jpeg|jpe|bmp|png)))", RegexOptions.IgnoreCase).Matches(HtmlContent);

            if (matchs.Count > 0)
            {
                if (!Directory.Exists(SaveFullPath))
                {
                    Directory.CreateDirectory(SaveFullPath);
                }
            }

            //准备处理要下载的文件
            int num = 0;
            foreach (Match match in matchs)
            {
                num++;
                string fileurl = match.Groups[1].Value.ToLower();

                if (fileurl.Contains("rorowo.com")) { continue; }

                string fileextname = Path.GetExtension(fileurl).ToLower();

                string newfilename = Guid.NewGuid().ToString() + fileextname;

                if (AutoMark)
                {
                    string tfilename = Guid.NewGuid().ToString() + "t" + fileextname;
                    //下载文件
                    client.DownloadFile(fileurl, SaveFullPath + tfilename);
                    ImageHelper.CreateWeaterPicture(SaveFullPath + tfilename, SaveFullPath + newfilename, HttpContext.Current.Server.MapPath(PicWaterMarkFile), fileextname);
                }
                else
                {
                    //下载文件
                    client.DownloadFile(fileurl, SaveFullPath + newfilename);
                }

                HtmlContent = HtmlContent.Replace(fileurl, PicUrl + PicSaveWebPath + newfilename);

                //添加到集合
                cData.FileList.Add(PicUrl + PicSaveWebPath + newfilename);
            }
            client.Dispose();

            cData.Content = HtmlContent;

            return cData;
        }
예제 #2
0
        public static ContentData SavePic(string HtmlContent, bool AutoMark)
        {
            ContentData cData = new ContentData();

            cData.Content  = string.Empty;
            cData.FileList = new List <string>();

            string PicWaterMarkFile = ConfigurationManager.AppSettings["PicWaterMarkFile"];
            string FileSavePath     = ConfigurationManager.AppSettings["FileSavePath"];
            string PicSaveWebPath   = ConfigurationManager.AppSettings["PicSaveWebPath"];
            string PicUrl           = ConfigurationManager.AppSettings["PicUrl"];

            string FileSubPath = string.Format("{0}/{1}/{2}/", DateTime.Now.ToString("yyyy"), DateTime.Now.ToString("MM"), DateTime.Now.ToString("dd"));

            PicSaveWebPath += FileSubPath;
            string SaveFullPath = FileSavePath + PicSaveWebPath.Replace("/", "\\");

            WebClient       client = new WebClient();
            MatchCollection matchs = new Regex(@"((http|https|ftp|rtsp|mms):(\/\/|\\\\){1}([\w\-]+[.]){1,}(net|com|cn|org|cc|tv|[0-9]{1,3})(([^.])+[.]{1}(gif|jpg|jpeg|jpe|bmp|png)))", RegexOptions.IgnoreCase).Matches(HtmlContent);

            if (matchs.Count > 0)
            {
                if (!Directory.Exists(SaveFullPath))
                {
                    Directory.CreateDirectory(SaveFullPath);
                }
            }

            //准备处理要下载的文件
            int num = 0;

            foreach (Match match in matchs)
            {
                num++;
                string fileurl = match.Groups[1].Value.ToLower();

                if (fileurl.Contains("rorowo.com"))
                {
                    continue;
                }

                string fileextname = Path.GetExtension(fileurl).ToLower();

                string newfilename = Guid.NewGuid().ToString() + fileextname;

                if (AutoMark)
                {
                    string tfilename = Guid.NewGuid().ToString() + "t" + fileextname;
                    //下载文件
                    client.DownloadFile(fileurl, SaveFullPath + tfilename);
                    ImageHelper.CreateWeaterPicture(SaveFullPath + tfilename, SaveFullPath + newfilename, HttpContext.Current.Server.MapPath(PicWaterMarkFile), fileextname);
                }
                else
                {
                    //下载文件
                    client.DownloadFile(fileurl, SaveFullPath + newfilename);
                }

                HtmlContent = HtmlContent.Replace(fileurl, PicUrl + PicSaveWebPath + newfilename);

                //添加到集合
                cData.FileList.Add(PicUrl + PicSaveWebPath + newfilename);
            }
            client.Dispose();

            cData.Content = HtmlContent;


            return(cData);
        }