using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class uploadimg : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["username"] == null)
        {
            Response.Redirect("login.aspx");
        }
        else
        {
            Image1.Visible = false;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            string name = FileUpload1.FileName;
            string size = FileUpload1.PostedFile.ContentLength.ToString();
            string type1 = FileUpload1.PostedFile.ContentType;
            string ipath = Server.MapPath("img\\") + name;
            string wpath = "img\\" + name;
            if (type1 != "jpg" || type1 != "gif" || type1 != "bmp" || type1 != "png")
            {
                Image1.Visible = true;
                FileUpload1.SaveAs(ipath);
                Image1.ImageUrl = wpath;
                Label1.Text = "<br>文件名是:" + name + "<br>文件大小是:" + size + "<br>文件类型是:" + type1 + "<br>本地文件路径是:" + ipath + "<br>相当路径是:" + wpath;
                SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["conn"]);
                conn.Open();
                SqlCommand cmd = new SqlCommand("insert into img(imgpath) values('" + wpath + "')", conn);
                cmd.ExecuteNonQuery();
                Label2.Text = "文件上传成功!";
                conn.Close();
            }
            else
            {
                Label1.Text = "不支持该文件类型!";
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex);
        }
    }
}图片是可以上传,但是如数据库的文件夹下有与正要上传的文件名同名就把前有的给替换了,请大侠请教把我的代码改正。不要太复杂哦。新手复杂了看不懂。谢谢!