Chinaunix首页 | 论坛 | 博客
  • 博客访问: 350606
  • 博文数量: 14
  • 博客积分: 2510
  • 博客等级: 少校
  • 技术积分: 1081
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-25 16:58
文章分类

全部博文(14)

文章存档

2011年(1)

2009年(2)

2008年(11)

我的朋友

分类:

2008-09-09 18:45:01

protected void UploadBtn_Click(object sender, EventArgs e)
    {
        //******************获取文件的扩展名如:(.rar)***************************
        string fileName = this.FileUpload1.PostedFile.FileName;//获取上传文件的名称
        int length = fileName.Length;//获取上传文件的名称的长度
        int intID = fileName.LastIndexOf(".");
        string oldName = fileName.Substring(intID, length - intID);//获得文件的扩展名
        //************************************************************************
        bool fileOK = false;
        string uploadPath = string.Empty;//上传文件的路径
        string uploadInfo = UploadTB.Text;//文件说明
        //获取根文件绝对路径
        string path = Server.MapPath("~/UserRes/" + "1" + "//");
        //如上传了文件,就判断文件格式
        FileUpload FU = FileUpload1;
        if (FileUpload1.HasFile)
        {
            string fileExtension = System.IO.Path.GetExtension(FU.FileName).ToLower();
            string[] allowedExtensions ={ ".gif", ".jpg", ".png", ".bmp", ".zip", ".rar", ".doc", ".xls", ".docx", ".swf", };
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                if (fileExtension == allowedExtensions[i])
                {
                    fileOK = true;
                }
            }
        }
        //判断文件是否过大
        if (FileUpload1.PostedFile.ContentLength > 5242880) //获取上载文件的字节大小
        {
            Response.Write("");
            return;
        }
        uploadPath = System.DateTime.Now.ToString("yyyyMMddhhmmss") + oldName;
        //调用saveas方法,实现上传文件
        if (fileOK)
        {
            try
            {
                FileUpload1.SaveAs(path + System.DateTime.Now.ToString("yyyyMMddhhmmss") + oldName);
                Response.Write("");
                UploadTB.Text = string.Empty;
            }
            catch(Exception error)
            {
                Response.Write("");
            }
        }
        else
        {
            Response.Write("");
            UploadTB.Text = string.Empty;
            return;
        }
    }
阅读(1009) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~