1.在TiImage.htm(这个页面是编辑器自带的页面)中当图片上传完毕后会提交到action="/UploadFile.html"
<form action="/UploadFile.html" method="post" name="spphotoform" target="uploadiframe" enctype="multipart/form-data">
1.1 控制器中的UploadFile方法中有个Common.Upload方法将图片保存到临时夹
public virtual object UploadFile() { string _img = string.Empty; HttpPostedFileBase postFile = Request.Files["userfile"] as HttpPostedFileBase; if (postFile.FileName != String.Empty) { _img = Common.Upload(postFile); } string js = "<script>setTimeout(function(){parent.geturl('" + _img + "');}, 400);</script>"; ViewData["msg"] = js; return View();// base.JavaScript(js); }
Common.Upload
public static string Upload(HttpPostedFileBase oFile) { string TemImageToPath = "/temp/" + DateTime.Now.Date.ToString("yyyy-MM") + "/"; string _tempPath = HttpContext.Current.Server.MapPath(TemImageToPath); if (!Directory.Exists(_tempPath)) { Directory.CreateDirectory(_tempPath); } string _ext = Path.GetExtension(oFile.FileName).ToLower(); string _name = Guid.NewGuid().ToString() + _ext; _tempPath = _tempPath + _name; if (oFile.ContentLength > 1024 * 1024) { MakeThumbnail(oFile, _tempPath, 500, 500, "HW");//缩略图 } else oFile.SaveAs(_tempPath); return TemImageToPath + _name; }
2. 当保存词条的时候,控制器CreateWord方法中 word.Content = word.Content.MovePic("wiki");(其中Wiki是保存到某个文件夹中)
public static string MovePic(this string content, string path) { if (string.IsNullOrEmpty(content)) return ""; //移动图片 string _t = DateTime.Now.Date.ToString("yyyy-MM"); string _path = appSettings.GetString("ImagePath") + "\\" + path + "\\" + _t + "\\"; if (_path.MoveImage(content)) { content = content.ReplaceContent("/temp/", "/" + path + "/"); content = content.ReplaceContent("edit"); } return content; }
public static bool MoveImage(this string _path, string _url) { MatchCollection mc; Regex _regex = new Regex(@"(/temp/)[\d]{4}-[\d]{2}/[\w-]+\.(gif|jpg)", RegexOptions.IgnoreCase); mc = _regex.Matches(_url); try { if (mc.Count > 0) { if (!Directory.Exists(_path)) { Directory.CreateDirectory(_path); } } for (int i = 0; i < mc.Count; i++) { string temp = mc[i].Value; string _target = _path + Regex.Replace(temp, @"(/temp/)[\d]{4}-[\d]{2}/", "", RegexOptions.IgnoreCase); File.Move(HttpContext.Current.Server.MapPath(temp), _target); } return true; } catch (Exception err) { } return false; }
public static string ReplaceContent(this string _content, string type) { if (string.IsNullOrEmpty(_content)) return ""; _content = Regex.Replace(_content, @"(/upload/)", "/", RegexOptions.IgnoreCase); MatchCollection mc; Regex _regex = new Regex(@"(href=|src=)/(wiki)/[\d]{4}(-|_)[\d]{2}/[\w-]+\.(gif|jpg)", RegexOptions.IgnoreCase); mc = _regex.Matches(_content); try { if (mc.Count > 0) { for (int i = 0; i < mc.Count; i++) { string temp = mc[i].Value; if (type == "show") { if (temp.Contains("href=")) _content = _content.Replace(temp, temp.Replace("href=", "href=" + appSettings.GetString("ImageUrl"))); else if (temp.Contains("src=")) _content = _content.Replace(temp, temp.Replace("src=", "src=" + appSettings.GetString("ImageUrl"))); } else if (type == "edit") { _content = _content.Replace(appSettings.GetString("ImageUrl"), ""); } } } else { _regex = new Regex(@"/(wiki)/[\d]{4}(-|_)[\d]{2}/[\w-]+\.(gif|jpg)", RegexOptions.IgnoreCase); MatchCollection _mc = _regex.Matches(_content); if (_mc.Count > 0) { for (int i = 0; i < _mc.Count; i++) { string _temp = _mc[i].Value; if (type == "show") { _content = _content.Replace(_temp, appSettings.GetString("ImageUrl") + _temp); } else if (type == "edit") { _content = _content.Replace(appSettings.GetString("ImageUrl"), ""); } } } } } catch (Exception err) { } return _content; }
public static string ReplaceContent(this string _content, string _tempUrl, string _targetUrl) { if (string.IsNullOrEmpty(_content)) return ""; Regex _regex = new Regex(_tempUrl, RegexOptions.IgnoreCase);
3. 当读取的词条的时候通过word.Content.UBB2Html().ReplaceContent("show");将图片的地址替换掉其中ImageUrl来自于配置文件
public static string UBB2Html(this string content) { Regex regex; Match match; //[IMG] regex = new Regex(@"(\[img\]([\s\S]*?)\[\/img\])", RegexOptions.IgnoreCase); for (match = regex.Match(content); match.Success; match = match.NextMatch()) { string img = "<div style=\"float: {0}; display: none;\" class=\"text_pic\"><a href={1} target=\"_blank\"><img class=editorImg title=\"{2}\" src={1} /></a><h3>{2}</h3></div>"; string _img = match.Groups[2].ToString() + " "; string _style = string.Empty; string _title = string.Empty; string _src = string.Empty; regex = new Regex(@"(style=FLOAT: ([\s\S]*?) )", RegexOptions.IgnoreCase); Match _match = regex.Match(_img); if (_match.Success) { _style = _match.Groups[2].ToString(); } regex = new Regex(@"(src=([\s\S]*?) )", RegexOptions.IgnoreCase); _match = regex.Match(_img); if (_match.Success) { _src = _match.Groups[2].ToString(); } regex = new Regex(@"(title=([\s\S]*?) )", RegexOptions.IgnoreCase); _match = regex.Match(_img); if (_match.Success) { _title = _match.Groups[2].ToString(); } content = content.Replace(match.Groups[0].ToString(), string.Format(img, _style, _src, _title)); } //\r\n\r\n content = content.Replace("\r\n\r\n", "<br /><div class=\"spctrl\"></div>"); //\r\n content = content.Replace("\r\n", "<br />"); //[b] regex = new Regex(@"(\[b\]([\s\S]*?)\[\/b\])", RegexOptions.IgnoreCase); for (match = regex.Match(content); match.Success; match = match.NextMatch()) { content = content.Replace(match.Groups[0].ToString(), "<b>" + match.Groups[2].ToString() + "</b>"); } //[i] regex = new Regex(@"(\[i\]([\s\S]*?)\[\/i\])", RegexOptions.IgnoreCase); for (match = regex.Match(content); match.Success; match = match.NextMatch()) { content = content.Replace(match.Groups[0].ToString(), "<i>" + match.Groups[2].ToString() + "</i>"); } //[url] regex = new Regex(@"(\[url\]([\s\S]*?)\[\/url\])", RegexOptions.IgnoreCase); for (match = regex.Match(content); match.Success; match = match.NextMatch()) { content = content.Replace(match.Groups[0].ToString(), "<a href='#'>" + match.Groups[2].ToString() + "</a>"); } //[sub] regex = new Regex(@"(\[sup\]([\s\S]*?)\[\/sup\])", RegexOptions.IgnoreCase); for (match = regex.Match(content); match.Success; match = match.NextMatch()) { content = content.Replace(match.Groups[0].ToString(), "<sup>" + match.Groups[2].ToString() + "</sup>"); } //[title] int i = 1; regex = new Regex(@"(\[title\]([\s\S]*?)\[\/title\])", RegexOptions.IgnoreCase); for (match = regex.Match(content); match.Success; match = match.NextMatch()) { //<h2 class="first"><a name="1"></a> //<h2 class=""><a name="2"></a> if (i == 1) content = content.Replace(match.Groups[0].ToString(), "<h2 class='first'><a name='1'></a>" + match.Groups[2].ToString() + "</h2>[title]"); else content = content.Replace(match.Groups[0].ToString(), "<h2 class=''><a name='" + i + "'></a>" + match.Groups[2].ToString() + "</h2>[title]"); i++; } //[title2] string[] resultString = Regex.Split(content, @"\[title\]", RegexOptions.IgnoreCase); content = string.Empty; i = 1; foreach (string str in resultString) { if (str.IndexOf("[title2]") != -1) { string _str = str.Title2(i - 1); content += _str; } else content += str; i++; } //[td] regex = new Regex(@"(\[td([\s\S]*?)\]([\s\S]*?)\[\/td\])", RegexOptions.IgnoreCase); for (match = regex.Match(content); match.Success; match = match.NextMatch()) { content = content.Replace(match.Groups[0].ToString(), "<td" + match.Groups[2].ToString() + "/>" + match.Groups[3].ToString() + "</td>"); } //[tr] regex = new Regex(@"(\[tr\]([\s\S]*?)\[\/tr\])", RegexOptions.IgnoreCase); for (match = regex.Match(content); match.Success; match = match.NextMatch()) { content = content.Replace(match.Groups[0].ToString(), "<tr>" + match.Groups[2].ToString() + "</tr>"); } //[table] regex = new Regex(@"(\[table\]([\s\S]*?)\[\/table\])", RegexOptions.IgnoreCase); for (match = regex.Match(content); match.Success; match = match.NextMatch()) { content = content.Replace(match.Groups[0].ToString(), "<table>" + match.Groups[2].ToString() + "</table>"); } content = content.Replace("\\\"", "\""); return content; }
public static string ReplaceContent(this string _content, string type) { if (string.IsNullOrEmpty(_content)) return ""; _content = Regex.Replace(_content, @"(/upload/)", "/", RegexOptions.IgnoreCase); MatchCollection mc; Regex _regex = new Regex(@"(href=|src=)/(wiki)/[\d]{4}(-|_)[\d]{2}/[\w-]+\.(gif|jpg)", RegexOptions.IgnoreCase); mc = _regex.Matches(_content); try { if (mc.Count > 0) { for (int i = 0; i < mc.Count; i++) { string temp = mc[i].Value; if (type == "show") { if (temp.Contains("href=")) _content = _content.Replace(temp, temp.Replace("href=", "href=" + appSettings.GetString("ImageUrl"))); else if (temp.Contains("src=")) _content = _content.Replace(temp, temp.Replace("src=", "src=" + appSettings.GetString("ImageUrl"))); } else if (type == "edit") { _content = _content.Replace(appSettings.GetString("ImageUrl"), ""); } } } else { _regex = new Regex(@"/(wiki)/[\d]{4}(-|_)[\d]{2}/[\w-]+\.(gif|jpg)", RegexOptions.IgnoreCase); MatchCollection _mc = _regex.Matches(_content); if (_mc.Count > 0) { for (int i = 0; i < _mc.Count; i++) { string _temp = _mc[i].Value; if (type == "show") { _content = _content.Replace(_temp, appSettings.GetString("ImageUrl") + _temp); } else if (type == "edit") { _content = _content.Replace(appSettings.GetString("ImageUrl"), ""); } } } } } catch (Exception err) { } return _content; }
这些图片专门部署在一个网站上:img.aqioo.com/wiki/2014-09/下面
浙公网安备 33010602011771号