private string UpImages(out int flag)
    {
        flag = 0;
        string subPath = String.Empty;
        if (this.upimage.Value == "")
        {
            model.NwCs_PicUrl = "blank";
        }
        else
        {
            string updatefilename = this.upimage.Value.ToString();
            string tempstring = null;
            if (updatefilename.IndexOf('.') > 0)
            {
                string[] name = updatefilename.Split('.');
                tempstring = name[name.Length - 1];
            }
            if (tempstring == "jpg" || tempstring == "JPG" || tempstring == "gif" || tempstring == "GIF")
            {
                string filePath = "/UpSPContract/";  //+System.DateTime.Now.ToString("yyyyMMdd")+"/";
                if (!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);    // 如无该文件夹,则创建
                }
                string fileName = "VS" + System.DateTime.Now.ToString("yyyyMMddhhmmss");
                string fileType = String.Empty;                string netPath = HttpContext.Current.Request.MapPath(HttpContext.Current.Request.ApplicationPath);
                CommonFile.FileHelper.SaveFiles(upimage, netPath + filePath, fileName, ref fileType);
                subPath = subPath + fileName + fileType;
                //ViewState["ConPath"] = netPath + filePath + subPath;
                //Response.Write("<script>alert('上传成功!')</script>");
            }
            else
            {
                flag = 1;
                Response.Write("<script>alert('图片格式不符合!')</script>");
            }
        }        return subPath;这里面的"/UpSPContract/";  是什么意思,是不是目录文件夹。然后数据是哪里读的。前台要去哪里读取。是不是应该去读数据库里面的。不过。我看了一个项目。好像都没有什么数据库操作。是不是可以直接读文件名。。我很菜啊。。

解决方案 »

  1.   

    这里面的"/UpSPContract/"是文件夹
    数据图片也是上传到服务器的文件夹下的,和数据库没关系
      

  2.   

    这个是上传到UpSPContract文件夹并返回图片的路径
      

  3.   

    郁闷,问问题前,先自己动下脑子,谁不是这样过来的,
    告诉你,详细运作过程,
    1,点击浏览按钮,选定好上传图片,不管图片是存放在什么位置的(存放在什么地方一点关系都没有)
    2,点击上传按钮,图片就会上传到/UpSPContract下,然后自动生成一个文件名
    (string filePath = "/UpSPContract/";  //+System.DateTime.Now.ToString("yyyyMMdd")+"/"; 就是这句起的作用)
    3,将取得图片地址,导入数据库
    4,附上代码碎片(如下)
    try
            {
                string test = Server.MapPath("ImageFiles/" + DateTime.Now.ToString("yyyy-MM-dd"));  //用来生成文件夹
                if (!Directory.Exists(test))
                {
                    Directory.CreateDirectory(test);
                }
                if (FileUpload1.PostedFile.FileName != "")
                {
                    string imgname = FileUpload1.PostedFile.FileName;
                    string imgType = imgname.Substring(imgname.LastIndexOf(".") + 1);
                    string quanname = DateTime.Now.ToString("yyyyMMddHHmmss") + imgname.LastIndexOf("\\") + "." + imgType;
                    if ("gif" != imgType && "jpg" != imgType && "GIF" != imgType && "JPG" != imgType)
                    {
                        Response.Write("<script>alert('请选择gif,jpg格式的文件!');</script>");
                        return;
                    }                string imgurl = "ImageFiles/" + DateTime.Now.ToString("yyyy-MM-dd") + "/" + quanname;
                    FileUpload1.PostedFile.SaveAs(Server.MapPath(imgurl));
                    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["con"]);
                    SqlCommand cmd = new SqlCommand("update students set image=@image where xuehao='002'", con);
                    cmd.Parameters.Add("@image", imgurl);
                    cmd.Connection.Open();
                    cmd.ExecuteNonQuery();
                    cmd.Connection.Close();
                    SqlCommand cmd1 = new SqlCommand("select * from students where xuehao='002'", con);
                    cmd1.Connection.Open();
                    SqlDataReader dr = cmd1.ExecuteReader();                if (dr.Read())
                    {
                        Response.Write("<script>alert('头像上传成功!');</script>");
                        string hui = dr["image"].ToString();
                        this.Image1.ImageUrl = hui;  
                    }
                    cmd1.Connection.Close();            }
                else
                {
                    Response.Write("<script>alert('请选择文件上传!');</script>");
                }
            }        catch (Exception ex)
            {
                throw ex;
            }
    string image=dr["image"].ToString();
                    if(!(image==""))
                    {
                        this.Image1.ImageUrl = image;
                    }