zoukankan      html  css  js  c++  java
  • .NET常用方法收藏

    1、过滤文本中的HTML标签

     1 /// <summary>  
     2     /// 清除文本中Html的标签  
     3     /// </summary>  
     4     /// <param name="Content"></param>  
     5     /// <returns></returns>  
     6     public static string ClearHtml(string Content)
     7     {
     8         Content = ReplaceHtml("&#[^>]*;", "", Content);
     9         Content = ReplaceHtml("</?marquee[^>]*>", "", Content);
    10         Content = ReplaceHtml("</?object[^>]*>", "", Content);
    11         Content = ReplaceHtml("</?param[^>]*>", "", Content);
    12         Content = ReplaceHtml("</?embed[^>]*>", "", Content);
    13         Content = ReplaceHtml("</?table[^>]*>", "", Content);
    14         Content = ReplaceHtml(" ", "", Content);
    15         Content = ReplaceHtml("</?tr[^>]*>", "", Content);
    16         Content = ReplaceHtml("</?th[^>]*>", "", Content);
    17         Content = ReplaceHtml("</?p[^>]*>", "", Content);
    18         Content = ReplaceHtml("</?a[^>]*>", "", Content);
    19         Content = ReplaceHtml("</?img[^>]*>", "", Content);
    20         Content = ReplaceHtml("</?tbody[^>]*>", "", Content);
    21         Content = ReplaceHtml("</?li[^>]*>", "", Content);
    22         Content = ReplaceHtml("</?span[^>]*>", "", Content);
    23         Content = ReplaceHtml("</?div[^>]*>", "", Content);
    24         Content = ReplaceHtml("</?th[^>]*>", "", Content);
    25         Content = ReplaceHtml("</?td[^>]*>", "", Content);
    26         Content = ReplaceHtml("</?script[^>]*>", "", Content);
    27         Content = ReplaceHtml("(javascript|jscript|vbscript|vbs):", "", Content);
    28         Content = ReplaceHtml("on(mouse|exit|error|click|key)", "", Content);
    29         Content = ReplaceHtml("<\?xml[^>]*>", "", Content);
    30         Content = ReplaceHtml("<\/?[a-z]+:[^>]*>", "", Content);
    31         Content = ReplaceHtml("</?font[^>]*>", "", Content);
    32         Content = ReplaceHtml("</?b[^>]*>", "", Content);
    33         Content = ReplaceHtml("</?u[^>]*>", "", Content);
    34         Content = ReplaceHtml("</?i[^>]*>", "", Content);
    35         Content = ReplaceHtml("</?strong[^>]*>", "", Content);
    36 
    37         Content = Regex.Replace(Content.Trim(), "\s+", " ");
    38         string clearHtml = Content;
    39         return clearHtml;
    40     }
    41 
    42     /// <summary>  
    43     /// 清除文本中的Html标签  
    44     /// </summary>  
    45     /// <param name="patrn">要替换的标签正则表达式</param>  
    46     /// <param name="strRep">替换为的内容</param>  
    47     /// <param name="content">要替换的内容</param>  
    48     /// <returns></returns>  
    49     private static string ReplaceHtml(string patrn, string strRep, string content)
    50     {
    51         if (string.IsNullOrEmpty(content))
    52         {
    53             content = "";
    54         }
    55         Regex rgEx = new Regex(patrn, RegexOptions.IgnoreCase);
    56         string strTxt = rgEx.Replace(content, strRep);
    57         return strTxt;
    58     }
    HTML标记过滤

    2、页面文字长度按像素截取

     1  public static string Cutstring(object obj, int pixelLength)
     2         {
     3      string value = obj == null ? "" : obj.ToString();
     4             if (pixelLength > 0)
     5             {
     6                 int maxLength = Convert.ToInt32(Math.Floor(pixelLength / 100.0 * 8));
     7                 int keepLength = maxLength == 0 ? maxLength : maxLength - 1;
     8                 if (value.Length > maxLength)
     9                 {
    10                     string endString = "...";
    11                     value = value.Substring(0, keepLength) + endString;
    12                 }
    13             }
    14             return value;
    15 }
    字符串截取

    3、FileUpload上传文件

    //使用方法
      string StudentPhoto = "";
       bool UploadResult = false;
     if (FileUp.HasFile)
                    {
                        StudentPhoto = UploadFile(FileUp, "UpLoad/System/StudentPhoto", new string[] { ".gif", ".png",".jpg" }, 2, 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) / 1024 / 1024;
            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 = 0; 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("0");
                    }
    View Code

    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, 0, 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;
            }
    View Code

    6、获取url字符串中指定参数值

      var urlRegex = System.Text.RegularExpressions.Regex.Match(visit.rf, @"(?:^|?|&)ssp=(.*)(?:&|$)");
       ssprf = urlRegex.Groups[1].ToString();
    View Code
  • 相关阅读:
    搭建无线漫游网络及需要注意的问题
    手机如何借用笔记本网络上网
    VM下Linux网卡丢失(pcnet32 device eth0 does not seem to be ...)解决方案
    安装VMware vCenter过程设置数据库方法
    无法连接vCenter Server清单https://IP:10443
    ESXi控制台TSM:弥补vSphere Client不足
    Shell中逻辑判断
    bash 编程中循环语句用法
    Shell中IFS用法
    Shell中的${},##和%%的使用
  • 原文地址:https://www.cnblogs.com/loyung/p/4625974.html
Copyright © 2011-2022 走看看