.NetͨÓÃÎļþÉÏ´«Àà(¿ÉÉÏ´«´óÎļþ£¬±¾µØÊÔ¹ý480M)
<asp:Image ID="imgLogo" runat="server" /><br/> <asp:Label ID="labMsg" runat="server" ForeColor="#CC0000"></asp:Label><br /> <asp:FileUpload ID="UploadLogo" runat="server" Width="60%" Height="22px"/> <asp:Button ID="btnUpload" runat="server" Text=" ÉÏ´« " onclick="btnUpload_Click"/>
.aspx.csÒ³Ãæ
// ÉÏ´«°´Å¥ protected void btnUpload_Click(object sender, EventArgs e) { UploadFile uploadFileObj = new UploadFile(); //ʵÀý»¯ÎļþÉÏ´«Àà uploadFileObj.MaxFileSize = 100; // ÉèÖÃÉÏ´«Îļþ×î´ó³¤¶È£¬µ¥Î»K uploadFileObj.FileType = "jpg|jpeg|gif|png"; // ÉèÖÃÔÊÐíÉÏ´«µÄÎļþÀàÐÍ string uploadPath = Server.MapPath("~/UploadFiles/Other/"); // ÉèÖÃÉÏ´«Ä¿Â¼È«Â·¾¶ uploadFileObj .UploadFileGo(uploadPath, UploadLogo); //ÎļþÉÏ´« labMsg.Text = uploadFileObj.UploadInfo; // ÉÏ´«ÏûÏ¢Ìáʾ if (uploadFileObj.UploadState == true) { imgLogo.ImageUrl = "~/UploadFiles/Other/" + uploadFileObj.NewFileName; // ÏÔʾͼƬ } }
UploadFile.cs ÎļþÉÏ´«Àà
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; namespace Common { /// <summary>ÎļþÉÏ´«Àà</summary> public class UploadFile { #region ×ֶΠprivate string _UploadInfo; // ÎļþÉÏ´«µÄ·µ»ØÐÅÏ¢¡£ private bool _UploadState; // ÎļþÉÏ´«µÄ·µ»Ø״̬¡£ private string _FileType; // ÔÊÐíÉÏ´«ÎļþµÄÀàÐÍ¡£ private int _FileSize; // ÉÏ´«ÎļþµÄ´óС£¬µ¥Î»B private int _MaxFileSize; // ÉÏ´«Îļþ´óСµÄ×î´ó³¤¶È£¬µ¥Î»B private string _NewFileName; // ÉÏ´«ºóµÄÎļþÃû¡£ #endregion /// <summary>³õʼÎļþÉÏ´«Àà(ĬÈÏ)</summary> public UploadFile() { _UploadInfo = "NONE"; _UploadState = false; _FileType = "*"; _MaxFileSize = 1024000;//1000k ¼´1024*1000b£¬µ¥Î»B _NewFileName = ""; } #region ÊôÐÔ /// <summary>ÎļþÉÏ´«µÄ·µ»ØÐÅÏ¢</summary> public string UploadInfo { set { _UploadInfo = value; } get { return _UploadInfo; } } /// <summary>ÎļþÉÏ´«µÄ·µ»Ø״̬£¬³É¹¦true£¬Ê§°Üfalse</summary> public bool UploadState { set { _UploadState = value; } get { return _UploadState; } } /// <summary>ÔÊÐíÉÏ´«ÎļþµÄÀàÐÍ,* ĬÈÏ´ú±í¿ÉÈÎÒâÀàÐÍ,»ò×Ô¶¨ÒåÀàÐÍ£¬Èç "jpg|gif|bmp"</summary> public string FileType { set { _FileType = value; } get { return _FileType; } } /// <summary>ÉÏ´«ÎļþµÄ´óС£¬µ¥Î»K</summary> public int FileSize { get { return _FileSize / 1024; } } /// <summary>ÉÏ´«Îļþ´óСµÄ×î´ó³¤¶È£¬µ¥Î»K</summary> public int MaxFileSize { set { _MaxFileSize = value * 1024; } get { return _MaxFileSize / 1024; } } /// <summary>ÉÏ´«ºóµÄÎļþÃû</summary> public string NewFileName { set { _NewFileName = value; } get { return _NewFileName; } } #endregion #region ÉÏ´«Ö÷³ÌÐò /// <summary>ÉÏ´«±¾µØÎļþµ½·þÎñÆ÷</summary> /// <param name="strSaveDir">ÔÚ·þÎñÆ÷¶Ë±£´æµÄÎïÀí·¾¶¡£</param> /// <param name="FileUploadCtrlID">ÉÏ´«µÄÎļþ¶ÔÏó£¬ÕâÀïʹÓõÄÊÇFileUpload¿Ø¼þ£¬</param> /// <param>µÚ¶þ¸ö²ÎÊýÈç¹ûÊÇHTMl Input(File)¿Ø¼þ¿É¸ÄΪ£ºHtmlInputFile HtmCtrlObjUploadFile</param> /// <returns></returns> public void UploadFileGo(string strSaveDir, FileUpload FileUploadCtrlID) { int intFileExtPoint = FileUploadCtrlID.PostedFile.FileName.LastIndexOf("."); //×îºóÒ»¸ö . ºÅµÄλÖà string strFileExtName = FileUploadCtrlID.PostedFile.FileName.Substring(intFileExtPoint + 1).ToLower(); // »ñÈ¡ÉÏ´«ÎļþµÄºó׺Ãû¡£ _FileSize = FileUploadCtrlID.PostedFile.ContentLength;//ÉÏ´«µÄÎļþ´óС byte if (_FileSize == 0) // ÅжÏÊÇ·ñÓÐÎļþÐèÒªÉÏ´«»òËùÑ¡ÎļþÊÇ·ñΪ0×Ö½Ú¡£ { _UploadInfo = "ûÓÐÑ¡ÔñÒªÉÏ´«µÄÎļþ»òËùÑ¡Îļþ´óСΪ0×Ö½Ú"; _UploadState = false; return; // ·µ»ØÎļþÉÏ´«×´Ì¬ºÍÐÅÏ¢¡£ } if (_FileSize > _MaxFileSize) // ÏÞÖÆÒªÉÏ´«µÄÎļþ´óС(byte)¡£ { _UploadInfo = "ÉÏ´«µÄÎļþ³¬¹ýÏÞÖÆ´óС(" + (_MaxFileSize / 1024).ToString() + "K)"; _UploadState = false; return; // ·µ»ØÎļþÉÏ´«×´Ì¬ºÍÐÅÏ¢¡£ } if (_FileType != "*") { if (_FileType.ToLower().IndexOf(strFileExtName.ToLower().Trim()) == -1) // ÅжÏÒªÉÏ´«µÄÎļþÀàÐ͵ÄÊÇ·ñÔÚÔÊÐíµÄ·¶Î§ÄÚ¡£ { _UploadInfo = "²»ÔÊÐíÉÏ´«µÄÎļþÀàÐÍ(ÔÊÐíµÄÀàÐÍ£º|" + _FileType + ")"; _UploadState = false; return; // ·µ»ØÎļþÉÏ´«×´Ì¬ºÍÐÅÏ¢ } } if (_NewFileName == "") { DateTime dteNow = DateTime.Now; // ¶¨ÒåÈÕÆÚ¶ÔÏó£¬ÎªÉÏ´«ºóµÄÎļþÃüÃû¡£ _NewFileName = dteNow.Year.ToString() + dteNow.Month.ToString() + dteNow.Day.ToString() + GetRandomStr(8); // Ëæ»úµØΪÉÏ´«ºóµÄÎļþÃüÃû,ÈÕÆÚ+Ëæ»úÊý¡£ _NewFileName = _NewFileName + "." + strFileExtName; //°üº¬À©Õ¹ÃûµÄÎļþÃû } FileUploadCtrlID.PostedFile.SaveAs(this.GetSaveDirectory(strSaveDir) + _NewFileName); // ÒÔеÄÎļþÃû±£´æÉÏ´«µÄÎļþµ½Ö¸¶¨ÎïÀí·¾¶¡£ _UploadInfo = "ÎļþÉÏ´«³É¹¦"; // ·µ»ØÉÏ´«ºóµÄ·þÎñÆ÷¶ËÎļþÎïÀí·¾¶¡£ _UploadState = true; } #endregion /// <summary>»ñÈ¡Ö¸¶¨Î»ÊýµÄËæ»úÊý</summary> /// <param name="RndNumCount">Ëæ»úÊýλÊý¡£</param> /// <returns></returns> private string GetRandomStr(int RndNumCount) { string RandomStr; RandomStr = ""; Random Rnd = new Random(); for (int i = 1; i <= RndNumCount; i++) { RandomStr += Rnd.Next(0, 9).ToString(); } return RandomStr; } /// <summary>»ñÈ¡ÉÏ´«Îļþ´æ·ÅĿ¼</summary> /// <param name="DirectoryPath">´æ·ÅÎļþµÄÎïÀí·¾¶¡£</param> /// <returns>·µ»Ø´æ·ÅÎļþµÄĿ¼¡£</returns> public string GetSaveDirectory(string DirectoryPath) { if (!Directory.Exists(DirectoryPath)) // Åжϵ±Ç°Ä¿Â¼ÊÇ·ñ´æÔÚ¡£ { Directory.CreateDirectory(DirectoryPath); // ½¨Á¢ÉÏ´«Îļþ´æ·ÅĿ¼¡£ } return DirectoryPath; } } #region ¸½£ºÐÞ¸ÄÉÏ´«´óСµÄÅäÖà /* ÐèÒªÐ޸ĵÄÊÇ ÔÚ C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\CONFIGĿ¼À ÕÒµ½ÎļþmaxRequestLength="4096" ½«ÖµÐ޸ĴóһЩ£¬ÀýÈ磺102400 Õâ¸ö²ÎÊýµÄµ¥Î»Ó¦¸ÃÊÇKBµÄ ÒÔÉÏ·½·¨ÊÇÐÞ¸ÄÈ«¾ÖµÄ£¬Èç¹û¹«ÐèÒªÐÞ¸ÄÒ»¸öÏîÄ¿£¬ÄÇôÊÇÐÞ¸ÄÏîÄ¿ÀïµÄWeb.configÎļþ ÔÚ<system.web></system.web>Ö®¼äÌí¼Ó£¬ <httpRuntime useFullyQualifiedRedirectUrl="true" maxRequestLength="21000" executionTimeout="300" /> ÆäÖУ¬ maxRequestLength£ºÉèÖÃÉÏ´«ÎļþµÄ×î´óÖµ£¬µ¥Î»£ºKB¡££¨Ä¬ÈÏÊÇ4096KB£¬¼´4M£© executionTimeout£ºÉèÖó¬Ê±Ê±¼ä£¬µ¥Î»£ºÃë¡££¨Ä¬ÈÏÊÇ90Ã룩 */ #endregion }
ÍƼöÐÅÏ¢
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- Asp.net MVCÔ´Âë·ÖÎö--Model Validation(Se....
- .Net À¬»ø»ØÊÕ»úÖÆÔÀíºÍËã·¨£¨Ò»£©
- .netʵÌåнâ
- ·ÖÎöASP.NET¶ÁÈ¡XMLÎļþ4ÖÖ·½·¨
- »ñÈ¡ÍøÕ¾ËõÂÔͼ´úÂë
- Asp.net ¸ù¾ÝIPµØÖ·»ñÈ¡¿çÍø¶ÎmacµØÖ·º¯Êý¡¾ËѲء¿
- ¹ýÂËÃô¸Ð¹Ø¼ü×Ö¡£
- Éú³ÉÖ¸¶¨Î»ÊýµÄËæ»ú×Ö·û´®
- ¿Í»§¶Ë»ñÈ¡MACµØÖ·£¬µ½·þÎñÆ÷ÑéÖ¤
- ²½²½ÎªÓª .NETÈý²ã¼Ü¹¹½âÎö ¶þ¡¢Êý¾Ý¿âÉè¼Æ
ÆÀÂÛ