.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个数组的常用方法和属性,并根据方 ...
随机推荐
- python--切片--6
原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 一.对list进行切片 取一个list的部分元素是非常常见的操作.比如,一个list如下: &g ...
- Mac上因磁盘格式导致gulp无限刷新问题
今天遇到个超奇葩的问题,使用gulp.watch监控文件变化,但是并没有修改文件,却一直执行change,导致浏览器无限刷新 调试了10小时,代码各种删改,一直不得其解.切换到Windows运行,又正 ...
- input、select等表单元素的对齐问题
今天在写页面时,发现了一个问题,当INPUT.SELECT及用图片做的button放在一起(并排放一起)时,没法子对齐,自己以不愿再加其他代码.也不愿使用JS来实现图片button的效果,试好半天,发 ...
- 《苹果开发之Cocoa编程》挑战2 创建一个数据源 练习
<苹果开发之Cocoa编程>第4版 P87 创建一个to-do list应用程序,在文本框中输入任务.当用户单击Add按钮时,添加字符串到一个变长队列,新任务就出现在list的末尾. 关键 ...
- ABBYY 识别结果的文档怎么导出
使用ABBYY FineReader Pro for Mac OCR文字识别软件识别文档时,识别结果可以保存至一个文件.复制到剪贴板或通过电子邮件发送.可以执行下列操作:导出整个文档.仅导出所选页面. ...
- 配置 Hdp 4 Window 中的一些问题
1,E0508: User [?] not authorized for WF job [-- jobid] 很明显验证问题, 修改 oozie-site.xml中节点为 <property&g ...
- OpenJudge计算概论-文字排版
/*====================================================================== 文字排版 总时间限制: 1000ms 内存限制: 65 ...
- 【转】WinForm不同版本覆盖安装
vs2005为winform程序做的安装包.在以有程序旧版本的机子上用新版本的安装包安装软件时提示 “以经安装该产品的另一个版本.无法继续安装此版本........” 在安装部署项目中设“Remove ...
- 64位python安装MySQL-python 1.2.5
在64位的python直接安装MySQL-python 1.2.5有问题,参考http://www.linuxfly.org/windows_install_mysql_python_library/ ...
- du命令 实现Linux 某个文件夹下的文件按大小排序
1. df -lh 2. du -s /usr/* | sort -rn这是按字节排序 3. du -sh /usr/* | sort -rn这是按兆(M)来排序 4.选出排在前面的10个du -s ...