上传图片后 显示上传成功后 可以加入数据库(sqlserver) 
并且可以在img控件上显示上传的图片(显示控件和上传控件 在一个页面上)

解决方案 »

  1.   

            string imagePath = "";
            if (FileUploadImage.HasFile)
            {
                string FileAndExtName = FileUploadImage.FileName;   //获取文件名 
                string ExtName = " ";
                string FileName = " ";
                string ContentType = FileUploadImage.PostedFile.ContentType;   //获取文件类型 
                string filepath = Server.MapPath("~/Upfile") + "\\";
                int i = FileAndExtName.LastIndexOf(".");   //取得文件扩展名
                string newext = FileAndExtName.Substring(i);
                //上传图片
                DateTime now = DateTime.Now;
                string newname = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
                if (CheckFileType(newext))
                {
                    if (!Directory.Exists(filepath))
                    {
                        Directory.CreateDirectory(filepath);
                        if (Path.HasExtension(FileAndExtName))
                        {
                            ExtName = Path.GetExtension(FileAndExtName);
                            FileName = FileAndExtName.Replace(ExtName, " ");
                        }
                        else
                        {
                            FileName = FileAndExtName;
                        }
                        FileUploadImage.SaveAs(filepath + newname + newext);
                    }
                    else
                    {
                        if (Path.HasExtension(FileAndExtName))
                        {
                            ExtName = Path.GetExtension(FileAndExtName);
                            FileName = FileAndExtName.Replace(ExtName, " ");
                        }
                        else
                        {
                            FileName = FileAndExtName;
                        }
                        FileUploadImage.SaveAs(filepath + newname + newext);
                    }
                }
                else
                {
                    Response.Write("<script>alert('图片格式不正确!')</script>");
                    return;
                }
                imagePath = "~/Upfile/" + newname + newext;
            }
            else
            {
                imagePath = "无";
            }
        bool CheckFileType(string ext)//判断上传图片扩展名
        {
            switch (ext.ToLower())
            {
                case ".gif":
                case ".png":
                case ".jpg":
                case ".jpeg":
                case ".bmp":
                    return true;
                default:
                    return false;
            }
        }
    这是显示图片的代码:
     function GetImagePath()
     {
        var upload = document.getElementById("ctl00_ContentPlaceHolder1_FileUploadImage");
        var image = document.getElementById("ctl00_ContentPlaceHolder1_Image1");
        var uploadValue = upload.value.toString();
    //debugger
        var i =uploadValue.lastIndexOf('.');
        var houZui = uploadValue.substring(i);
        if((houZui.toLowerCase()!=".jpg")&&(houZui.toLowerCase()!=".bmp"))
        {
            alert("您输入的图片格式不正确");return false;
        }
        image.src=upload.value;    
     }
      

  2.   

    if (CheckFileType(newext)) 
                { 
                    if (!Directory.Exists(filepath)) 
                    { 
                        Directory.CreateDirectory(filepath); 
                        if (Path.HasExtension(FileAndExtName)) 
                        { 
                            ExtName = Path.GetExtension(FileAndExtName); 
                            FileName = FileAndExtName.Replace(ExtName, " "); 
                        } 
                        else 
                        { 
                            FileName = FileAndExtName; 
                        } 
                        FileUploadImage.SaveAs(filepath + newname + newext); 
                    } 
                    else 
                    { 
                        if (Path.HasExtension(FileAndExtName)) 
                        { 
                            ExtName = Path.GetExtension(FileAndExtName); 
                            FileName = FileAndExtName.Replace(ExtName, " "); 
                        } 
                        else 
                        { 
                            FileName = FileAndExtName; 
                        } 
                        FileUploadImage.SaveAs(filepath + newname + newext); 
                    } 
                } 
      

  3.   

    哦,你是不是不知道哪个是上传控件?
    FileUpLoad