Ejemplo n.º 1
0
    protected void bt_add_Click(object sender, EventArgs e)
    {
        if (Can_Insert())
        {
            string filefullname = ((FileUpload)FileUpload1.FindControl("FileUpload1")).FileName; //获取文件获取路径和文件名
            string fillName     = filefullname.Substring(filefullname.LastIndexOf("\\") + 1);    //获取文件名
            string type         = filefullname.Substring(filefullname.LastIndexOf(".") + 1);     //获取文件类型
            if (type == "png" || type == "bmp" || type == "jpg" || type == "gif" || type == "PNG" ||
                type == "JPG" || type == "BMP" || type == "GIF")                                 //判断文件类型
            {
                //判断是否有该路径
                string wantPath = Server.MapPath("../img");
                if (!Directory.Exists(wantPath))
                {
                    Directory.CreateDirectory(wantPath);        //若没有路径就创建新的路径
                }
                ((FileUpload)FileUpload1.FindControl("FileUpload1")).SaveAs(Server.MapPath("../img/") +
                                                                            fillName);
                //上传服务器(获取服务器项目地址是Server.MapPath())
                Response.Write("<script>alert('添加成功!!')</script>");
            }
            else
            {
                Response.Write("<script>alert('图片上传错误!请检查文件名或路径')</script>");
            }
        }

        else
        {
            Response.Write("<script>alert('插入失败!!')</script>");
        }
    }