.NET常用方法收藏
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();
.NET常用方法收藏的更多相关文章
- POI操作Excel常用方法总结 分类: B1_JAVA 2013-08-23 10:01 349人阅读 评论(0) 收藏
转载自:http://blog.csdn.net/xjun15/article/details/5805429 一. POI简介 Apache POI是Apache ...
- jquey easyui 常用方法
jquey easyui 常用方法 2015-05-31 13:02 4473人阅读 评论(0) 收藏 举报 版本:1.4.2 一.easyui -textbox: 1.去空格: $('#tt1'). ...
- StringUtils工具类的常用方法
StringUtils 方法的操作对象是 java.lang.String 类型的对象,是对 JDK 提供的 String 类型操作方法的补充,并且是 null 安全的(即如果输入参数 String ...
- django基础 -- 3. urls.py view.py 参数 别名 重定向 常用方法 静态文件
一.基本格式 from django.conf.urls import url from . import views #循环urlpatterns,找到对应的函数执行,匹配上一个路径就找到对应的函数 ...
- Delphi FastReport报表常用方法
Delphi FastReport报表常用方法 作者及来源: EasyPass - 博客园 收藏到→_→: 摘要: Delphi FastReport报表常用方法 点击这里! ...
- Go 收藏
Golang 定位解决分布式系统,服务器应用开发,主要竞争对手是 Java.Python 之类:Rust 定位解决单机安全问题,高性能场景偏系统底层开发,主要竞争对手就是 C 和 C++. Golan ...
- HTTP请求的常用方法有哪些
HTTP请求的常用方法有:GET方法.POST方法.HEAD方法.PUT方法.DELETE方法.CONNECT方法.OPTIONS方法.TRACE方法.下面本篇文章就给大家介绍具体介绍一下HTTP请求 ...
- phpExcel常用方法详解【附有php导出excel加超级链接】
phpExcel常用方法详解[附有php导出excel加超级链接] 发表于4年前(-- :) 阅读() | 评论() 0人收藏此文章, 我要收藏 赞0 http://www.codeplex.com/ ...
- JavaScript数组方法速查,32个数组的常用方法和属性
JavaScript数组方法速查手册极简版 http://30ke.cn/doc/js-array-method JavaScript数组方法速查手册极简版中共收了32个数组的常用方法和属性,并根据方 ...
随机推荐
- LVS+keepalived配置
一.系统环境准备: 1.keepalive主服务器 主机名称:dir 系统环境:CentOS release 6.5 (Final) 外网ip:192.168.1.203(网络模式桥接) vip:19 ...
- POJ 1426 Find The Multiple --- BFS || DFS
POJ 1426 Find The Multiple 题意:给定一个整数n,求n的一个倍数,要求这个倍数只含0和1 参考博客:点我 解法一:普通的BFS(用G++能过但C++会超时) 从小到大搜索直至 ...
- 数据库必会必知 之 SQL四种语言:DDL DML DCL TCL(转)
今天群里面讨论,DDL 还是 DML,我这种小白还是总结下他们的区别吧. 1. DDL – Data Definition Language 数据库定义语言:定义数据库的结构. 其主要命令有CREAT ...
- Android—进度条
layout文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...
- Windows Phone使用总结(长期更新)
[感受和经历] 1,型号,撸妹640XL: 2,经历,去银行办卡,当然各种潜规则要我装APP,然后小妹夺我手机要帮我安装,拿过去之后又还给我了--哈哈哈哈,不过乐极生悲,我以为能成功躲过去了,发现中国 ...
- 共享内存+互斥量实现linux进程间通信 分类: Linux C/C++ 2015-03-26 17:14 67人阅读 评论(0) 收藏
一.共享内存简介 共享内存是进程间通信中高效方便的方式之一.共享内存允许两个或更多进程访问同一块内存,就如同 malloc() 函数向不同进程返回了指向同一个物理内存区域的指针,两个进程可以对一块共享 ...
- Tutorial: Triplet Loss Layer Design for CNN
Tutorial: Triplet Loss Layer Design for CNN Xiao Wang 2016.05.02 Triplet Loss Layer could be a tri ...
- Driver development
Windows Driver Kit (WDK) https://msdn.microsoft.com/en-us/library/windows/hardware/ff557573(v=vs.85) ...
- Linux-IP地址后边加个/8(16,24,32)是什么意思?
是掩码的位数 A类IP地址的默认子网掩码为255.0.0.0(由于255相当于二进制的8位1,所以也缩写成“/8”,表示网络号占了8位); B类的为255.255.0.0(/16) ...
- Oracle数据库—— 存储过程与函数的创建
一.涉及内容 1.掌握存储过程与函数的概念. 2.能够熟练创建和调用存储过程与函数. 二.具体操作 1.创建存储过程,根据职工编号删除scott.emp表中的相关记录. (1)以scott 用户连接数 ...