Ext.net中如何上传文件

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

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

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

            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("~/FileUpload/" + sNewName);
if (!Directory.Exists(Path.GetDirectoryName(strPath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(strPath));
}
this.UploadFile.PostedFile.SaveAs(strPath);
}

通过简单的操作,就是给上传的图片重新命名,并且保存到要保存的文件夹中。   


posted @ 2012-03-23 19:16  然嗄  阅读(4711)  评论(0编辑  收藏  举报