#region = SafeAddQueryToURL =
///
/// Add a query to an URL.
/// if the URL has not any query,then append the query key and value to it.
/// if the URL has some queries, then check it if exists the query key already,replace the value, or append the key and value
/// if the URL has any fragment, append fragments to the URL end.
///
///
/// string s = "http://blog.csdn.net/leewhoee/?a=1&b=2&c=3#tag";
/// WL(SafeRemoveQueryFromURL("a",s));
/// WL(SafeRemoveQueryFromURL("b",s));
/// WL(SafeRemoveQueryFromURL("c",s));
/// WL(SafeAddQueryToURL("d","new",s));
/// WL(SafeAddQueryToURL("a","newvalue",s));
/// 輸出如下:
/// http://blog.csdn.net/leewhoee/?b=2&c=3#tag
/// http://blog.csdn.net/leewhoee/?a=1&c=3#tag
/// http://blog.csdn.net/leewhoee/?a=1&b=2#tag
/// http://blog.csdn.net/leewhoee/?a=1&b=2&c=3&d=new#tag
/// http://blog.csdn.net/leewhoee/?a=newvalue&b=2&c=3#tag
///
public static string SafeAddQueryToURL(string key, string value, string url)
{
int fragPos = url.LastIndexOf("#");
string fragment = string.Empty;
if (fragPos > -1)
{
fragment = url.Substring(fragPos);
url = url.Substring(0, fragPos);
}
int querystart = url.IndexOf("?");
if (querystart < 0)
{
url += "?" + key + "=" + value;
}
else
{
Regex reg = new Regex(@"(?<=[&\?])" + key + @"=[^\s&#]*", RegexOptions.Compiled);
if (reg.IsMatch(url))
url = reg.Replace(url, key + "=" + value);
else
url += "&" + key + "=" + value;
}
return url + fragment;
}
#endregion
#region = SafeRemoveQueryFromURL =
///
/// Remove a query from url
///
///
///
///
public static string SafeRemoveQueryFromURL(string key, string url)
{
Regex reg = new Regex(@"[&\?]" + key + @"=[^\s&#]*&?", RegexOptions.Compiled);
return reg.Replace(url, new MatchEvaluator(PutAwayGarbageFromURL));
}
private static string PutAwayGarbageFromURL(Match match)
{
string value = match.Value;
if (value.EndsWith("&"))
return value.Substring(0, 1);
else
return string.Empty;
}
#endregion
致此,“浏覽服務器端圖片或文件”部分就完成了。
上傳圖片或文件
上圖:
因為這個彈框是CKEditZ喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcszhuam1xKOsztLDx9a70OjSqrSmwO3H68fzoaPPwsPmyse0psDtV0VCx+vH87XEQUNUSU9OKL/J0tTTw8jOus7T79HUtKbA7dCpx+vH86Oswt+8rcnPtPPM5c/gzawpo7oKPGJyPgoKPHByZSBjbGFzcz0="brush:java;">[HttpPost]
public ActionResult FileUpload()
{
var ckEditorFuncNum = Request.QueryString["CKEditorFuncNum"];
var type = Request.QueryString["Type"];
var isImage = !string.IsNullOrEmpty(type) && type.Equals("Images", StringComparison.InvariantCultureIgnoreCase);
var maxContentLength = isImage ? 512*1024 : 1024*1024;
var file = Request.Files["upload"];
if (file == null)
{
return Content("No file has been chosen!");
}
if (file.ContentLength > maxContentLength)
{
return Content("The image file size should be no bigger than 512KB! The document file size should be no bigger than 1024KB!");
}
var urlpath = string.Empty;
var datestamp = DateTime.Now.ToString("MMddyyyy");
var rootfolderpath = isImage ? "/Images/" : "/docs/";
var folderpath = Server.MapPath(rootfolderpath) + datestamp;
if (file.ContentLength > 0)
{
var filename = Path.GetFileNameWithoutExtension(file.FileName);
var fileextension = Path.GetExtension(file.FileName);
var timestamp = DateTime.Now.ToString("MMddyyyyHHmmssfff");
var filepath = string.Format("{0}/{1}{2}{3}", folderpath, filename, timestamp,
fileextension);
urlpath = string.Format("{4}{0}/{1}{2}{3}", datestamp, filename, timestamp,
fileextension, rootfolderpath);
if (!Directory.Exists(folderpath))
{
Directory.CreateDirectory(folderpath);
}
file.SaveAs(filepath);
}
return
Content(
string.Format(
@"<script type=""text/javascript"">window.parent.CKEDITOR.tools.callFunction({0}, ""{1}"");</script>",
ckEditorFuncNum, urlpath));
}
如果你不懂ASP.NET MVC,下面做一些簡單的解釋。[HttpPost]表明只處理POST請求,return Content("")是返回一個純文本字符串,其它部分大同小異。唯一值得注意的是最後返回了一段javascript腳本給CKEditor,它促使窗口跳轉到Image Info選項卡並把圖片URL傳遞過去,然後預覽。如下圖: