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;
    }

解决方案 »

  1.   

    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"); //创建了vs+年月日时分秒的文件名
                    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>"); //操.用Response.Write写script,这个比上面的垃圾代码还要垃圾!!!建议用 ClientScript.registerStartupScript
                } 
                else 
                { 
                    flag = 1; //原来flag是用来记录上传是否成功的,估计更新数据库时要判断这个值.
                    Response.Write(" <script>alert('图片格式不符合!') </script>"); 
                } 
            }         return subPath; 
        }
    //结束.
    //这段代码太垃圾.建议别用