• Asp.net相关知识和经验的碎片化记录


    1、web前台页面文件上传的文件大小限制问题

      1.1 通过Web.Config配置上传文件的大小限制(涉及站点安全问题,谨慎配置!!!)。

    <system.webServer>
      <security>
        <requestFiltering>
          <requestLimits maxQueryString="10240" maxAllowedContentLength="2147483647" /> <!—限制2G-->
        </requestFiltering>
      </security>
    </system.webServer>

      1.2 通过后台C#代码进行上传文件大小的限制。

    /* 判断上传的文件大小是否超出限制的大小 */
    public bool isOutFileSizeLimit(System.Web.HttpContext context,int nFileSize)
    {
        if(context.Request.ContentLength>nFileSize)
        {
            return true; //超出限制
        }
        else
        {
            return false; //未超出限制
        }
    }

      上述两种方法的使用可满足不同的需求,并不是可以相互替代的解决方法。新手总结,还望读者自行甄别使用。

     2、.net线程池的应用

     1 using System.Threading;
     2 
     3 namespace Threading_Test.Test
     4 {
     5     /// <summary>
     6     ///线程池处理函数输入参数类的封装
     7     /// </summary>
     8     internal class SynDataParam
     9     {
    10         public bool bIsSysData;     //标识
    11         public string sFolderPath;  //ftp路径
    12         public string sFolderPath_Tk;   //ftp路径tk
    13 
    14         /// <summary>
    15         /// 构造函数
    16         /// </summary>
    17         /// <param name="p_bIsSysData">标识</param>
    18         /// <param name="p_sFolderPath">ftp路径</param>
    19         /// <param name="p_sFolderPath_Tk">ftp路径tk</param>
    20         public SynDataParam(bool p_bIsSysData, string p_sFolderPath,string p_sFolderPath_Tk)
    21         {
    22             bIsSysData = p_bIsSysData;  
    23             sFolderPath = p_sFolderPath;
    24             sFolderPath_Tk = p_sFolderPath_Tk;
    25         }
    26     }
    27 
    28     public class Test
    29     {
    30           public void main()
    31          {
    32             ThreadPool.QueueUserWorkItem(new WaitCallback(this.SynDataToTiKu), new SynDataParam(true,l_sPartContentFtpPath,"123"));
    33          }
    34 
    35         /// <summary>
    36         /// 
    37         /// </summary>
    38         /// <param name="state">传入SynDataParam类型的参数</param>
    39         public void SynDataToTiKu(Object state)
    40         {
    41             SynDataParam SynDataParam=state as SynDataParam;
    42 
    43             if (SynDataParam.bIsSysData) 
    44             {
    45
    46             }
    47         }
    48 
    49     }
    50 }

     3、web service的http请求调用方式(不通过vs的服务引用功能)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.IO;
    using System.Web.Configuration;
    using System.Net;
    namespace yu.zhi.hui { public class GetOrSetDataByWebService { /***web service的http post方式调用***/ /// <summary> /// 获取结果(这个方法主要是获取PostUrl 然后调用GetPostRequest) /// </summary> /// <param name="sQueryStr">接口的输入参数,形如:sLevel=string&sFormat=string</param> /// <param name="interfaceName">需要调用的对方接口方法名</param> /// <returns>返回接口的返回值</returns> public string GetOrSetDataByWsWithHttpPost(string sQueryStr, string interfaceName,string sWsPath) { //post调用方式的url:"http://xxx.xxx.xx.xx:xxxx/xxxx.asmx/Get_xxx" string PostUrl = sWsPath + "/" + interfaceName; byte[] data = Encoding.UTF8.GetBytes(sQueryStr.ToString()); string resCode = GetPostRequest(data, PostUrl); return resCode; } /// <summary> /// Http Post方式调用ws接口--发送请求数据并获取响应数据 /// </summary> /// <param name="data"></param> /// <param name="url"></param> /// <returns></returns> private string GetPostRequest(byte[] data, string url) { try { //创建请求 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);//完整的请求地址(ip:端口号/+url) myRequest.Method = "POST"; myRequest.ContentType = "application/x-www-form-urlencoded"; myRequest.Accept = "text/xml"; myRequest.Headers.Add("SOAPAction", url);//是否和请求一起发送 myRequest.UseDefaultCredentials = true; myRequest.ContentLength = data.Length;//创建输入流 Stream newStream = myRequest.GetRequestStream(); //发送请求 Send the data. newStream.Write(data, 0, data.Length); newStream.Close(); /*-请求end-*/ /*-响应begin-*/ //创建响应 Get response var response = (HttpWebResponse)myRequest.GetResponse(); using (var reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8"))) { //读取响应 string result = reader.ReadToEnd(); reader.Close(); response.Close(); return result; } } catch (Exception ex) { return ex.ToString(); } } } }

     4、直接保存字符串到ftp服务器上的文件中

        /// <summary>
            /// 保存字符串到ftp的文件中
            /// </summary>
            /// <param name="sStr">待保存字符串</param>
            /// <param name="sFtpUrl">ftp服务器的url</param>
            /// <param name="sFilePath">文件路径</param>
            /// <param name="sFtpUser">ftp服务器用户名</param>
            /// <param name="sFtpPwd">ftp服务器密码</param>
            public bool SaveStringToFileOnFtp(string sStr, string sFtpUrl,string sFilePath,string sFtpUser,string sFtpPwd)
            {
                try
                {
                    //上传到ftp服务器
                    FtpWebRequest reqFTP;
                    Uri uri = new Uri(sFtpUrl + sFilePath);
                    string sPartialPathOfFile = sFilePath.Substring(0, sFilePath.LastIndexOf("/") + 1);
                    //创建文件所在文件夹
                    FtpCheckDirectoryExist(sFtpUrl, sPartialPathOfFile, sFtpUser, sFtpPwd);
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);
                    reqFTP.Credentials = new NetworkCredential(sFtpUser, sFtpPwd);
                    reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
                    reqFTP.KeepAlive = false;
                    reqFTP.UseBinary = true;
                    reqFTP.Proxy = null;
                    int buffLength = 2048;  //每次读入文件流2kb
                    byte[] buff = new byte[buffLength];
                    //将字符串读入内存
                    MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(sStr));
                    Stream requestStream = reqFTP.GetRequestStream();
                    int len = stream.Read(buff, 0, buff.Length);
                    while (len > 0)
                    {
                        requestStream.Write(buff, 0, len);
                        len = stream.Read(buff, 0, buffLength);
                    }
                    stream.Close();
                    requestStream.Close();
                    stream.Dispose();//释放资源
                    requestStream.Dispose();//释放资源
                    return true;
                }
                catch(Exception ex)               
                {
                    ex.ToString();
                    return false;
                }
            }
          /// <summary>
            /// 判断文件的目录是否存,不存则创建
            /// </summary>
            /// <param name="destFilePath"></param>
            private static void FtpCheckDirectoryExist(string strFtpURI, string destFilePath, string strFtpUserID, string strFtpPassword)
            {
                string fullDir = destFilePath.Substring(0, destFilePath.LastIndexOf("/"));
                string[] dirs = fullDir.Split('/');
                string curDir = "";
                for (int i = 0; i < dirs.Length; i++)
                {
                    string dir = dirs[i];
                    //如果是以/开始的路径,第一个为空
                    if (dir != null && dir.Length > 0)
                    {
                        try
                        {
                            curDir += dir + "/";
                            FtpMakeDir(strFtpURI, curDir, strFtpUserID, strFtpPassword);
                        }
                        catch (Exception)
                        { }
                    }
                }
            }
            /// <summary>
            /// 创建ftp目录
            /// </summary>
            private static Boolean FtpMakeDir(string strFtpURI, string destFilePath, string strFtpUserID, string strFtpPassword)
            {
                FtpWebRequest req = (FtpWebRequest)WebRequest.Create(strFtpURI + destFilePath);
                req.Credentials = new NetworkCredential(strFtpUserID, strFtpPassword);
                req.Proxy = null;
                req.KeepAlive = false;
                req.Method = WebRequestMethods.Ftp.MakeDirectory;//请求方法为创建目录方法
                req.UseBinary = true;
                FtpWebResponse response = req.GetResponse() as FtpWebResponse;
                Stream ftpStream = response.GetResponseStream();
                ftpStream.Close();
                response.Close();
                req.Abort();
                return true;
            }  

     ......

    作者:yuzhihui
    出处:http://www.cnblogs.com/yuzhihui/
    声明:欢迎任何形式的转载,但请务必注明出处!!!
  • 相关阅读:
    nginx中的超时配置
    ubuntu系统TCP连接参数优化-TIME_WAIT过多解决办法
    python中的raise用法
    pip安装各种模块
    ubuntu上安装python的ldap模块
    docker 打印带时间的日志
    基于Xilinx Zynq Z7045 SoC的CNN的视觉识别应用
    VIVADO下IBERT使用指南
    菜根谭全文及译文
    bit,Byte,Word,DWORD(DOUBLE WORD,DW)
  • 原文地址:https://www.cnblogs.com/yuzhihui/p/5337347.html
Copyright © 2020-2023  润新知