protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpimage.HasFile == false)
        {
            //Response.Write("<script lanage='javascript'> alert('请指定上传的头像')</script>");
            string clientScript = "var falseimage=parent.document.getElementById('falseimage');$(falseimage).html('请指定上传的头像');";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "falseimage", clientScript, true);        }
        else
        {
            string fType = FileUpimage.PostedFile.ContentType;//获取图像的类型
            if (fType == "image/bmp" || fType == "image/gif" || fType == "image/pjpeg" || fType == "image/jpeg" || fType == "image/x-png")
            {
                //获取文件信息
                FileInfo file = new FileInfo(FileUpimage.PostedFile.FileName);
                ///随机数据
                Guid guid = Guid.NewGuid();
                string stamp = guid.ToString("N");
                //生成随机数
                Random aa = new Random();
                string number = aa.Next(10000).ToString();
                //原始图片保存路径                string path = "/Upload/UpImage/" + stamp + ".gif";
                //缩略图保存路径
                string spath = "Upload/UpImage/" + number + ".gif";
                Session["iamgeurl"] = spath.ToString();                try
                {
                    //原始图片保存this.FileUpimage.PostedFile.FileName
                    FileUpimage.SaveAs(Server.MapPath(path));
                    //缩略图保存
                    MakeThumbImage(Server.MapPath(path), Server.MapPath(spath), 200, 100);
                    //给隐藏的图片控件赋值并显示
                    //  Image1.Visible = true;
                    //Image1.ImageUrl = spath;
                    string clientScript = "var userInfo1=parent.document.getElementById('userInfo');$(userInfo1).attr('src','Upload/UpImage/" + stamp + ".gif" + "');$(userInfo1).show();";
                         string clients = "var userInfo3=parent.document.getElementById('userInf');$(userInfo3).attr('src','Upload/UpImage/" + number + ".gif" + "');$(userInfo3).show();";
                    clientScript += "var falseimage=parent.document.getElementById('falseimage');$(falseimage).html('上传成功,管理员会尽快进行审核.........');";
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "successimage", clientScript, true);
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "sdfsdf", clients, true);
                }
                catch
                {
                    //Response.Write("<script lanage='javascript'> alert('上传失败')</script>");
                    string clientScript = "var falseimage=parent.document.getElementById('falseimage');$(falseimage).html('上传失败');";
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "falseimage", clientScript, true);
                }
            }
            else
            {
                //Response.Write("<script lanage='javascript'> alert('上传头像格式不正确')</script>");
                string clientScript = "var falseimage=parent.document.getElementById('falseimage');$(falseimage).html('上传头像格式不正确');";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "falseimage", clientScript, true);
            }
        }
    }
不进try  但是同样的 代码 在远程服务器上就 可以上传  

解决方案 »

  1.   

    if (FileUpimage.HasFile)
    {}
    else{}
    本地单步跟踪
    类型判断<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="FileUpload1"   
      ErrorMessage="必须是jpg或者gif文件" ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.jpg|.Jpg|.gif|.Gif)$"></asp:RegularExpressionValidator>   或
    string extn = Path.GetExtension(FileUpload1.PostedFile.FileName);//获取后缀
    if (Regex.IsMatch(extn, @".jpg|.png|.gif|.rar|.txt", RegexOptions.IgnoreCase))
    {
        }
      

  2.   

    这个 上传错误和 您 说的 这个格式 有联系吗 我上传的就jpg的
      

  3.   

     private Int32 FileLength = 0;
        OleDbConnection oledbcon = null;
        protected void Page_Load(object sender, EventArgs e)
        {
        }    protected void Button1_Click(object sender, EventArgs e)
        {        try
            {
                oledbcon = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;data source=" + HttpContext.Current.Server.MapPath(@"~/App_Data/dbs.mdb"));
               
                string title = FunStr(TB_name.Text.ToString());
                string content = FunStr(TB_info.Text.ToString());
                int  ptype = 7;
                string FileName = myFile.Value;
                HttpPostedFile UpFile = myFile.PostedFile; //获取对由客户端指定的上传文件的访问
                FileLength = UpFile.ContentLength;//获取上传文件的字节大小
                if (FileLength == 0)
                {
                    oledbcon.Open();
                    OleDbCommand cmd1 = new OleDbCommand( "insert into FZ_product(Name,Tid,Info)values('" + title  + "','" +ptype  + "','" + content  + "')", oledbcon);
                    cmd1.ExecuteNonQuery();
                    Response.Write("<script>alert('数据添加成功');window.location.href='product_add.aspx.aspx'</script>");
                    oledbcon.Close();
                }
                else
                {
                    string exName = FileName.Substring(FileName.LastIndexOf(".") + 1).ToUpper();//截取图片的后缀名
                    if (exName == "JPG" || exName == "BMP" || exName == "GIF")//判断图片的类型
                    {
                        if (FileLength > 204800)//判断图片是否大于200k(根据自己的需要判断大小)
                        {
                            Response.Write("<script>alert('对不起,图片大小不能大于200K');window.location.href='product_add.aspx'</script>");
                            return;
                        }
                        else
                        {
                            string ImageName = DateTime.Now.ToString("yyyyMMddhhmmssfff") + "." + exName;//图片名称设置为保存的时间
                            Byte[] FileByte = new Byte[FileLength]; //图象文件储存到数组  
                            Stream ObjectStream = UpFile.InputStream;//建立数据流对像,获取一个 Stream 对象,该对象指向一个上载文件,以准备读取该文件的内容。                        ObjectStream.Read(FileByte, 0, FileLength); //读取图象文件数据
                            string StrSql = "Insert Into FZ_product(Name,Tid,Img,Info,addtime) Values('" + title  + "','"+ptype +"',@ImageName,'" + content + "','"+DateTime .Now .ToString ()+"')";
                            OleDbCommand Cmd = new OleDbCommand(StrSql, oledbcon);
                            //Cmd.Parameters.Add("@Image",OleDbType.Binary, FileLength).Value = FileByte;
                            Cmd.Parameters.Add("@ImageName", OleDbType.VarChar, 100).Value = ImageName;
                            oledbcon.Open();
                            this.myFile.PostedFile.SaveAs(Server.MapPath("../upload") + "\\" + ImageName);
                            Cmd.ExecuteNonQuery();
                            oledbcon.Close();
                            Response.Write("<script>alert('数据添加成功');window.location.href='product_add.aspx'</script>");
                        }
                    }
                    else
                    {
                        Response.Write("<script>alert('对不起,请选择正确的的图片!');window.location.href='product_add.aspx'</script>");
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                Response.Write("<script>alert('" + ex.Message + "')</script>");
            }
        }
        public static string FunStr(string str)
        {
            str = str.Replace("'", "‘");
            str = str.Replace("   ", "&nbsp;");        str = str.Trim();
            if (str.Trim().ToString() == "")
                str = "无";
            return str;
        } 
    你参考下
      

  4.   

    http://download.csdn.net/source/2488098,有个简单的例子,可以参考哈,打开就可以看了