1、过滤文本中的HTML标签
/// <summary>
/// 清除文本中Html的标签
/// </summary>
/// <param name="Content"></param>
/// <returns></returns>
public static string ClearHtml(string Content)
{
Content = ReplaceHtml("&#[^>]*;", "", Content);
Content = ReplaceHtml("</?marquee[^>]*>", "", Content);
Content = ReplaceHtml("</?object[^>]*>", "", Content);
Content = ReplaceHtml("</?param[^>]*>", "", Content);
Content = ReplaceHtml("</?embed[^>]*>", "", Content);
Content = ReplaceHtml("</?table[^>]*>", "", Content);
Content = ReplaceHtml(" ", "", Content);
Content = ReplaceHtml("</?tr[^>]*>", "", Content);
Content = ReplaceHtml("</?th[^>]*>", "", Content);
Content = ReplaceHtml("</?p[^>]*>", "", Content);
Content = ReplaceHtml("</?a[^>]*>", "", Content);
Content = ReplaceHtml("</?img[^>]*>", "", Content);
Content = ReplaceHtml("</?tbody[^>]*>", "", Content);
Content = ReplaceHtml("</?li[^>]*>", "", Content);
Content = ReplaceHtml("</?span[^>]*>", "", Content);
Content = ReplaceHtml("</?div[^>]*>", "", Content);
Content = ReplaceHtml("</?th[^>]*>", "", Content);
Content = ReplaceHtml("</?td[^>]*>", "", Content);
Content = ReplaceHtml("</?script[^>]*>", "", Content);
Content = ReplaceHtml("(javascript|jscript|vbscript|vbs):", "", Content);
Content = ReplaceHtml("on(mouse|exit|error|click|key)", "", Content);
Content = ReplaceHtml("<\\?xml[^>]*>", "", Content);
Content = ReplaceHtml("<\\/?[a-z]+:[^>]*>", "", Content);
Content = ReplaceHtml("</?font[^>]*>", "", Content);
Content = ReplaceHtml("</?b[^>]*>", "", Content);
Content = ReplaceHtml("</?u[^>]*>", "", Content);
Content = ReplaceHtml("</?i[^>]*>", "", Content);
Content = ReplaceHtml("</?strong[^>]*>", "", Content); Content = Regex.Replace(Content.Trim(), "\\s+", " ");
string clearHtml = Content;
return clearHtml;
} /// <summary>
/// 清除文本中的Html标签
/// </summary>
/// <param name="patrn">要替换的标签正则表达式</param>
/// <param name="strRep">替换为的内容</param>
/// <param name="content">要替换的内容</param>
/// <returns></returns>
private static string ReplaceHtml(string patrn, string strRep, string content)
{
if (string.IsNullOrEmpty(content))
{
content = "";
}
Regex rgEx = new Regex(patrn, RegexOptions.IgnoreCase);
string strTxt = rgEx.Replace(content, strRep);
return strTxt;
}
HTML标记过滤
2、页面文字长度按像素截取
public static string Cutstring(object obj, int pixelLength)
{
string value = obj == null ? "" : obj.ToString();
if (pixelLength > )
{
int maxLength = Convert.ToInt32(Math.Floor(pixelLength / 100.0 * ));
int keepLength = maxLength == ? maxLength : maxLength - ;
if (value.Length > maxLength)
{
string endString = "...";
value = value.Substring(, keepLength) + endString;
}
}
return value;
}
字符串截取
3、FileUpload上传文件
//使用方法
string StudentPhoto = "";
bool UploadResult = false;
if (FileUp.HasFile)
{
StudentPhoto = UploadFile(FileUp, "UpLoad/System/StudentPhoto", new string[] { ".gif", ".png",".jpg" }, , out UploadResult);
if (!UploadResult)
{
//上传不成功操作
string UpMsg = "$('#IsRefresh').val('TwoNotice');art.dialog({title:'提示',icon:'warning',content:'" + StudentPhoto + "'});";
Page.ClientScript.RegisterStartupScript(ClientScript.GetType(), "upmsg", UpMsg, true);
}
else
{
//上传成功后操作
} /// <summary>
/// 指定上传控件到指定路径
/// </summary>
/// <param name="Upload1">上传控件</param>
/// <param name="UpPath">上传路径,对于站点路径,如:UpLoad/System</param>
/// <param name="FileType">文件后缀{ ".gif", ".png", ".bmp", ".jpg" }</param>
/// <param name="FileSize">上传文件大小单位MB</param>
/// <param name="UploadMessage">是否上传成功</param>
/// <returns>上传成功返回路径,失败返回原因</returns>
protected string UploadFile(FileUpload Upload1, string UpPath, string[] FileType, double FileSize, out bool UploadMessage)
{
UploadMessage = false;
string UpFile = "";
string FileMessage = "";
string strName = Upload1.PostedFile.FileName;
double filesize = Convert.ToDouble(Upload1.PostedFile.ContentLength) / / ;
string Extensions = "";
if (strName != "")
{
string fileExtension = System.IO.Path.GetExtension(Upload1.FileName).ToLower();
string newName = Guid.NewGuid().ToString();
string juedui = Server.MapPath("~/" + UpPath + "/");
string newFileName = juedui + newName + fileExtension;
if (Upload1.HasFile)
{
//验证文件格式
string[] allowedExtensions = FileType;
bool FileEx = false;
for (int j = ; j < allowedExtensions.Length; j++)
{
Extensions += allowedExtensions[j] + ";";
if (fileExtension == allowedExtensions[j])
{
FileEx = true;
}
}
if (!FileEx)
{
FileMessage += "文件上传失败,错误原因:按指定格式上传(" + Extensions + ");";
}
//验证文件大小
if (filesize > FileSize)
{
FileMessage += string.Format("请按照指定文件大小上传,文件限定{0}MB,当前文件[{1}]共{2}MB", FileSize, strName, String.Format("{0:F}", filesize));
} if (FileMessage == "")
{
try
{
if (!Directory.Exists(juedui))
{
Directory.CreateDirectory(juedui);
}
Upload1.PostedFile.SaveAs(newFileName);
UpFile = "../" + UpPath + "/" + newName + fileExtension;
UploadMessage = true;
}
catch (Exception EX)
{
UploadMessage = false;
FileMessage += EX.Message;
}
}
else
{
UploadMessage = false;
}
}
}
else
{
FileMessage += "照片文件选择已过期,请重新选择;";
}
if (UploadMessage)
return UpFile;
else
return FileMessage;
}
FileUpload上传文件
4、异步上传服务端文件保存
HttpPostedFile file = Request.Files["Filedata"];//上传文件对象
string uploadPath = Server.MapPath("~/UpLoad/QueAttach/");
if (file != null)
{
if (!Directory.Exists(uploadPath))
{
Directory.CreateDirectory(uploadPath);
}
string ExtName = Path.GetExtension(file.FileName);
string FileName= file.FileName .Replace(ExtName,"");
string fileName = "【" + FileName + "】" + DateTime.Now.ToString("yyyyMMddHHmmss") + ExtName;
file.SaveAs(uploadPath + fileName);
//下面这句代码缺少的话,上传成功后上传队列的显示不会自动消失
Response.Write(FileName);
}
else
{
Response.Write("");
}
5、POST数据提交
/// <summary>
/// POST提交数据接收字符json
/// </summary>
/// <param name="url">远程服务器路径</param>
/// <param name="postData">提交数据</param>
/// <returns>接收数据</returns>
protected static string PostData(string url, byte[] postData)
{
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = postData.Length; Stream newStream = myRequest.GetRequestStream();
// Send the data.
newStream.Write(postData, , postData.Length);
newStream.Close(); // Get response
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
return reader.ReadToEnd();
} //调用方法
/// <summary>
/// 获取直播录制信息
/// </summary>
/// <param name="webcastId">直播ID</param>
/// <returns>录制视频</returns>
public RecordInfo Get_Live_Transcribe(string webcastId)
{
RecordInfo jsonOut = null;
try
{
string Url = "http://";
string LocalData = "loginName=admin%40ruiyoutech.com&password=ruiyoutech&sec=&webcastId=" + webcastId;
string strJsonInput = PostData(Url, System.Text.Encoding.Default.GetBytes(LocalData));
JavaScriptSerializer serializer = new JavaScriptSerializer();
jsonOut = serializer.Deserialize<RecordInfo>(strJsonInput);
}
catch(Exception e)
{ }
return jsonOut;
}
6、获取url字符串中指定参数值
var urlRegex = System.Text.RegularExpressions.Regex.Match(visit.rf, @"(?:^|\?|&)ssp=(.*)(?:&|$)");
ssprf = urlRegex.Groups[].ToString();