http://www.cnblogs.com/fishtreeyu/archive/2011/01/19/1939286.html
.aspx页面
1 | <asp:Image ID= "imgLogo" runat= "server" /><br/> |
2 | <asp:Label ID= "labMsg" runat= "server" ForeColor= "#CC0000" ></asp:Label><br /> |
3 | <asp:FileUpload ID= "UploadLogo" runat= "server" Width= "60%" Height= "22px" /> |
4 | <asp:Button ID= "btnUpload" runat= "server" Text= " 上传 " onclick= "btnUpload_Click" /> |
.aspx.cs页面
02 | protected void btnUpload_Click( object sender, EventArgs e) |
04 | UploadFile uploadFileObj = new UploadFile(); |
05 | uploadFileObj.MaxFileSize = 100; |
06 | uploadFileObj.FileType = "jpg|jpeg|gif|png" ; |
08 | string uploadPath = Server.MapPath( "~/UploadFiles/Other/" ); |
09 | uploadFileObj .UploadFileGo(uploadPath, UploadLogo); |
10 | labMsg.Text = uploadFileObj.UploadInfo; |
11 | if (uploadFileObj.UploadState == true ) |
13 | imgLogo.ImageUrl = "~/UploadFiles/Other/" + uploadFileObj.NewFileName; |
UploadFile.cs 文件上传类
003 | using System.Configuration; |
005 | using System.Web.Security; |
007 | using System.Web.UI.WebControls; |
008 | using System.Web.UI.WebControls.WebParts; |
009 | using System.Web.UI.HtmlControls; |
014 | /// <summary>文件上传类</summary> |
015 | public class UploadFile |
020 | private string _UploadInfo; |
021 | private bool _UploadState; |
022 | private string _FileType; |
023 | private int _FileSize; |
024 | private int _MaxFileSize; |
025 | private string _NewFileName; |
031 | /// <summary>初始文件上传类(默认)</summary> |
034 | _UploadInfo = "NONE" ; |
035 | _UploadState = false ; |
037 | _MaxFileSize = 1024000; |
042 | /// <summary>文件上传的返回信息</summary> |
043 | public string UploadInfo |
045 | set { _UploadInfo = value; } |
046 | get { return _UploadInfo; } |
049 | /// <summary>文件上传的返回状态,成功true,失败false</summary> |
050 | public bool UploadState |
052 | set { _UploadState = value; } |
053 | get { return _UploadState; } |
056 | /// <summary>允许上传文件的类型,* 默认代表可任意类型,或自定义类型,如 "jpg|gif|bmp"</summary> |
057 | public string FileType |
059 | set { _FileType = value; } |
060 | get { return _FileType; } |
063 | /// <summary>上传文件的大小,单位K</summary> |
066 | get { return _FileSize / 1024; } |
069 | /// <summary>上传文件大小的最大长度,单位K</summary> |
070 | public int MaxFileSize |
072 | set { _MaxFileSize = value * 1024; } |
073 | get { return _MaxFileSize / 1024; } |
076 | /// <summary>上传后的文件名</summary> |
077 | public string NewFileName |
079 | set { _NewFileName = value; } |
080 | get { return _NewFileName; } |
087 | /// <summary>上传本地文件到服务器</summary> |
088 | /// <param name="strSaveDir">在服务器端保存的物理路径。</param> |
089 | /// <param name="FileUploadCtrlID">上传的文件对象,这里使用的是FileUpload控件,</param> |
090 | /// <param>第二个参数如果是HTMl Input(File)控件可改为:HtmlInputFile HtmCtrlObjUploadFile</param> |
091 | /// <returns></returns> |
092 | public void UploadFileGo( string strSaveDir, FileUpload FileUploadCtrlID) |
094 | int intFileExtPoint = FileUploadCtrlID.PostedFile.FileName.LastIndexOf( "." ); |
095 | string strFileExtName = FileUploadCtrlID.PostedFile.FileName.Substring(intFileExtPoint + 1).ToLower(); |
097 | _FileSize = FileUploadCtrlID.PostedFile.ContentLength; |
101 | _UploadInfo = "没有选择要上传的文件或所选文件大小为0字节" ; |
102 | _UploadState = false ; |
106 | if (_FileSize > _MaxFileSize) |
108 | _UploadInfo = "上传的文件超过限制大小(" + (_MaxFileSize / 1024).ToString() + "K)" ; |
109 | _UploadState = false ; |
113 | if (_FileType != "*" ) |
115 | if (_FileType.ToLower().IndexOf(strFileExtName.ToLower().Trim()) == -1) |
117 | _UploadInfo = "不允许上传的文件类型(允许的类型:|" + _FileType + ")" ; |
118 | _UploadState = false ; |
123 | if (_NewFileName == "" ) |
125 | DateTime dteNow = DateTime.Now; |
126 | _NewFileName = dteNow.Year.ToString() + dteNow.Month.ToString() + dteNow.Day.ToString() + GetRandomStr(8); |
127 | _NewFileName = _NewFileName + "." + strFileExtName; |
129 | FileUploadCtrlID.PostedFile.SaveAs( this .GetSaveDirectory(strSaveDir) + _NewFileName); |
130 | _UploadInfo = "文件上传成功" ; |
138 | /// <summary>获取指定位数的随机数</summary> |
139 | /// <param name="RndNumCount">随机数位数。</param> |
140 | /// <returns></returns> |
141 | private string GetRandomStr( int RndNumCount) |
145 | Random Rnd = new Random(); |
146 | for ( int i = 1; i <= RndNumCount; i++) |
148 | RandomStr += Rnd.Next(0, 9).ToString(); |
156 | /// <summary>获取上传文件存放目录</summary> |
157 | /// <param name="DirectoryPath">存放文件的物理路径。</param> |
158 | /// <returns>返回存放文件的目录。</returns> |
159 | public string GetSaveDirectory( string DirectoryPath) |
161 | if (!Directory.Exists(DirectoryPath)) |
163 | Directory.CreateDirectory(DirectoryPath); |
165 | return DirectoryPath; |