1、web前臺頁面文件上傳的文件大小限制問題 1.1 通過Web.Config配置上傳文件的大小限制(涉及站點安全問題,謹慎配置!!!)。 1.2 通過後臺C#代碼進行上傳文件大小的限制。 上述兩種方法的使用可滿足不同的需求,並不是可以相互替代的解決方法。新手總結,還望讀者自行甄別使用。 2、.ne ...
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請求調用方式(不通過IDE的服務引用功能)
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 { internal class GetOrSetDataHelper { /***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伺服器上的文件中
#region 保存字元串到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> /// <param name="localFile"></param> /// <returns></returns> 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; }
#endregion
後續更新中,敬請期待...