[置顶] Ext.Net 1.x_Ext.Net.FileUpload上传文件

今天在使用ext.net的UploadField控件想上传文件时,发现examples.ext.net官网上的例子写的不是很详细。于是通过网上找资料,结合asp.net的文件上传的方法,终于实现了图片的上传功能。以下就是实现的代码,供大家参考!

首先在.aspx文件中插入一个文件上传的控件:

        <ext:FileUploadField ID="UploadFile" runat="server" FieldLabel="上传附件"  Width="300" ButtonText="浏览..." Icon="Attach" />

然后是.cs文件中实现上传的具体代码:

/// <summary>
    /// 上传附件
    /// </summary>
    protected void Upload(object sender, DirectEventArgs e)
    {
        string UploadFile = "";
        if (this.UploadFile.HasFile)
        {
            UploadFile = this.UploadFile.PostedFile.FileName.ToString();
            int FileSize = Int32.Parse(this.UploadFile.PostedFile.ContentLength.ToString());
            if (FileSize > 5 * 1024 * 1024)
            {
                X.Msg.Alert("提示信息", "上传文件过大!").Show();
                return;
            }
            string strFileName = Path.GetExtension(this.UploadFile.PostedFile.FileName).ToUpper();//获取文件后缀              
            if (!(strFileName == ".BMP" || strFileName == ".GIF" || strFileName == ".JPG"))
            {
                X.Msg.Alert("提示信息", "文件格式不正确!").Show();
                return;
            }

            Random ran = new Random();
            string sNewName = DateTime.Now.ToString(@"yyyyMMddHHmmss") + ran.Next(100, 999)
              + Path.GetExtension(this.UploadFile.PostedFile.FileName);
            string strPath = Server.MapPath("~/Uploads/ZmQuo/"+System.DateTime.Now .Year .ToString () +"/"+ sNewName);
            if (!Directory.Exists(Path.GetDirectoryName(strPath)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(strPath));
            }
            try
            {
                this.UploadFile.PostedFile.SaveAs(strPath);
                X.Msg.Alert("提示信息", "文件上传成功!").Show();
                return;
            }
            catch(Exception ex) 
            {
                throw ex; 
            }
        }
    }
[置顶] Ext.Net 1.x_Ext.Net.FileUpload上传文件_第1张图片

你可能感兴趣的:(exception,String,upload,Random,asp.net)