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常用方法收藏的更多相关文章

  1. POI操作Excel常用方法总结 分类: B1_JAVA 2013-08-23 10:01 349人阅读 评论(0) 收藏

    转载自:http://blog.csdn.net/xjun15/article/details/5805429     一. POI简介               Apache POI是Apache ...

  2. jquey easyui 常用方法

    jquey easyui 常用方法 2015-05-31 13:02 4473人阅读 评论(0) 收藏 举报 版本:1.4.2 一.easyui -textbox: 1.去空格: $('#tt1'). ...

  3. StringUtils工具类的常用方法

    StringUtils 方法的操作对象是 java.lang.String 类型的对象,是对 JDK 提供的 String 类型操作方法的补充,并且是 null 安全的(即如果输入参数 String ...

  4. django基础 -- 3. urls.py view.py 参数 别名 重定向 常用方法 静态文件

    一.基本格式 from django.conf.urls import url from . import views #循环urlpatterns,找到对应的函数执行,匹配上一个路径就找到对应的函数 ...

  5. Delphi FastReport报表常用方法

    Delphi FastReport报表常用方法   作者及来源: EasyPass - 博客园    收藏到→_→:   摘要: Delphi FastReport报表常用方法       点击这里! ...

  6. Go 收藏

    Golang 定位解决分布式系统,服务器应用开发,主要竞争对手是 Java.Python 之类:Rust 定位解决单机安全问题,高性能场景偏系统底层开发,主要竞争对手就是 C 和 C++. Golan ...

  7. HTTP请求的常用方法有哪些

    HTTP请求的常用方法有:GET方法.POST方法.HEAD方法.PUT方法.DELETE方法.CONNECT方法.OPTIONS方法.TRACE方法.下面本篇文章就给大家介绍具体介绍一下HTTP请求 ...

  8. phpExcel常用方法详解【附有php导出excel加超级链接】

    phpExcel常用方法详解[附有php导出excel加超级链接] 发表于4年前(-- :) 阅读() | 评论() 0人收藏此文章, 我要收藏 赞0 http://www.codeplex.com/ ...

  9. JavaScript数组方法速查,32个数组的常用方法和属性

    JavaScript数组方法速查手册极简版 http://30ke.cn/doc/js-array-method JavaScript数组方法速查手册极简版中共收了32个数组的常用方法和属性,并根据方 ...

随机推荐

  1. python数据结构与算法——小猫钓鱼(使用队列)

    按照<啊哈>里的思路实现这道题目,但是和结果不一样,我自己用一幅牌试了一下,发现是我的结果像一点,可能我理解的有偏差. # 小猫钓鱼 # 计算桌上每种牌的数量 # 使用defaultdic ...

  2. Linux下删除乱码或特殊字符文件

    今天遇到一个问题,一个文件名是“-MXV9.log”,直接用rm删除的时候就报错,如下: [localhost]rm -MXV9.log rm: illegal option -- M rm: ill ...

  3. banner轮播图js

    例子1: if(!$('.side_ul ul').is(":animated")){            var wli = $('.side_ul li').width()+ ...

  4. Jquery判断滚动条是否到达窗口顶部和底部

    <script type="text/javascript"> $(document).ready(function(){     alert($(window).he ...

  5. dbcp基本配置和重连配置 -- mysql 8小时自动断开连接的问题

    1. 引入dbcp (选择1.4) Java代码   com.alibaba.external jakarta.commons.dbcp 1.4 2. dbcp的基本配置 相关配置说明: initia ...

  6. Java 操作Excel 之Poi(第一讲)

    1.Poi 简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能.HSSF - 提供读写Micros ...

  7. js的数组操作 splice

    原文:点击打开链接 1.作用:从指定位置删除部分元素并增加新的元素                1.1.该方法返回值是被删除的元素组成的数组                1.2.splice是直接 ...

  8. 【转】DBMS_STATS.GATHER_TABLE_STATS详解 2012-04-22 09:20:10

    [转]DBMS_STATS.GATHER_TABLE_STATS详解 2012-04-22 09:20:10 分类: Linux 由于Oracle的优化器是CBO,所以对象的统计数据对执行计划的生成至 ...

  9. vb6 webbrowser 事件捕获

    Private WithEvents htmlDocument As htmlDocument Private WithEvents btnCompute As MSHTML.HTMLButtonEl ...

  10. Google将数十亿行代码储存在单一的源码库

    过去16年,Google使用一个中心化源码控制系统去管理一个日益庞大的单一共享源码库.它的代码库包含了约10亿个文件(有重复文件和分支)和 3500万行注解,86TB数据,900万唯一源文件中含有大约 ...