文件上传(上传后改变上传文件名和创建保存上传文件的目录)

    protected void UpFlies_Click(object sender, EventArgs e)
    {
        if (fuDocFile.HasFile)
        {
            string name = fuDocFile.PostedFile.FileName;                  // 客户端文件路径
            System.IO.FileInfo file = new System.IO.FileInfo(name);
            string fileName = file.Name;                                    // 原文件名称
            string upfileName;                                    // 上传文件名称
            string da = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString()+DateTime.Now.Hour.ToString()+DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString();
            string OrignFolder =DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();  //文件保存的目录名称
            string fileExtension = System.IO.Path.GetExtension(fileName);   //取文件的扩展名
            if (fileExtension == ".doc" || fileExtension == ".gif" || fileExtension == ".jpg" || fileExtension == ".rar" || fileExtension == ".zip" || fileExtension == ".pdf" || fileExtension == ".xls" || fileExtension == ".txt")
            {
                upfileName = da + fileExtension; //获取上传文件的文件名
                if (!Directory.Exists(Server.MapPath("./UpFiles/") + OrignFolder)) //判断上传文件路径是否存在,若不存在,否则创建。
                Directory.CreateDirectory(Server.MapPath("./UpFiles/") + OrignFolder);  //创建目录
                string webFilePath = Server.MapPath("./UpFiles/" + OrignFolder+"/" + upfileName);    // 服务器端文件路径
                fuDocFile.SaveAs(webFilePath);                                // 使用 SaveAs 方法保存文件
                int strSize = fuDocFile.PostedFile.ContentLength / 1024; //上传文件的大小
                string strPath = "UpFiles/" + fileName; //上传文件的路径
                string typeurl = " ";
                switch (fileExtension)
                {
                    case ".gif": typeurl = "~/images/gif.gif"; break;
                    case ".jpg": typeurl = "~/images/jpg.gif"; break;
                    case ".rar": typeurl = "~/images/rar.gif"; break;
                    case ".zip": typeurl = "~/images/rar.gif"; break;
                    case ".pdf": typeurl = "~/images/pdf.gif"; break;
                    case ".doc": typeurl = "~/images/doc.gif"; break;
                    case ".xls": typeurl = "~/images/xls.gif"; break;
                    case ".txt": typeurl = "~/images/txt.gif"; break;
                }
                lbupdatemessage.Text = "上传文件的类型" + typeurl + "<br> 上传文件的大小:" + strSize + "(KB)<br> 上传文件的路径:" + strPath + "<br> 上传文件的原始名称:" + fileName + "<br>上传文件名:" + upfileName; ;
            }
            else
            {
                lbmesg.Visible = true;
            }
        }
        else
        {
            Response.Write("<script language='javascript'>alert('请点击浏览,选择要上传的文件!')</script>");
        }
    }

转载于:https://www.cnblogs.com/zyming/archive/2008/10/25/1319483.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值