.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个数组的常用方法和属性,并根据方 ...
随机推荐
- JavaScript 编写多线程代码引用Concurrent.Thread.js(转)
这是一个很简单的功能实现: <script type="text/javascript" src="Concurrent.Thread.js">&l ...
- UVa 1586 Molar mass --- 水题
UVa 1586 题目大意:给出一种物质的分子式(不带括号),求分子量.本题中分子式只包含4种原子,分别为C.H.O.N, 原子量分别为12.01,1.008,16.00,14.01 解题思路:先实现 ...
- Sublime Text 转
距第一篇的开箱水文,已经有4个月的时间了,但因为懒,就没有下文了.终于,今天,我觉得写一篇准技术文章了. 忘记了是怎么开始用的ST,应该是在网上看到别人推荐才用到吧,用了有半年了.在windows下是 ...
- P141 实战练习——字符串(修改后)
1.在项目中创建Number类,判断字符串“mingrikejijavabu”中字符‘i’出现了几次,并将结果输出. 方法一: // String str="mingrikejijavabu ...
- kuangbin_ShortPath K (POJ 3159)
很简单的模板题 放在K那么后的位置的原因大概是 光看题意并不是很容易想到是用最短路解吧 奈何kuangbin分在了最短路专题 一发水过 #include <iostream> #inclu ...
- java的nio之:java的nio系列教程之DatagramChannel
Java NIO中的DatagramChannel是一个能收发UDP包的通道.因为UDP是无连接的网络协议,所以不能像其它通道那样读取和写入.它发送和接收的是数据包. 打开 DatagramChann ...
- HTTPS-透彻学习汇总
SSL和SSH和OpenSSH,OpenSSL有什么区别 一.SSL的作用 不使用SSL/TLS的HTTP通信,就是不加密的通信.所有信息明文传播,带来了三大风险. 窃听风险(eavesdroppin ...
- Integer.parseInt()和Integer.valueOf()有什么区别
jdk的源代码的时候注意到Integer.parseInt(s) 和 Integer.valueOf(s)的具体代码的实现有所区别: Java代码 public static int parseInt ...
- 关于a标签的target属性
超级链接a的target属性已经是不被新规范支持了,其值有四个保留字: 1._blank <a href="document.html" target=" ...
- x2go
单词解析 productivity n. 生产力:生产率:生产能力seamlessly adv. 无缝地roam constantly 经常漫游agility and flex ...