只允许上传 GIF JPG类型的文件   我在本地调试的时候是好的,但是放到服务器上就出现这个问题,都上传的同一张图片

解决方案 »

  1.   

    HttpPostedFile hpf = FileUpload1.PostedFile;
                //取得文件名(不含路径)            char[] splitChar = { };            string[] FilenameArray = hpf.FileName.Split(splitChar);            string Filename = FilenameArray[FilenameArray.Length - 1].ToLower();            if (hpf.FileName.Length < 1)
                {                Response.Write("请选择您要上传的图片文件");                return;            }            if (hpf.ContentType != "image/pjpeg" && hpf.ContentType != "image/gif")
                {                Response.Write("只允许上传 GIF JPG类型的文件");                return;            }            else
                {                System.Text.StringBuilder sb = new System.Text.StringBuilder();                sb.Append(DateTime.Now.Year.ToString());                sb.Append(DateTime.Now.Month.ToString());                sb.Append(DateTime.Now.Day.ToString());                sb.Append(DateTime.Now.Hour.ToString());                sb.Append(DateTime.Now.Minute.ToString());                sb.Append(DateTime.Now.Second.ToString());                if (Filename.ToLower().EndsWith("gif"))
                    {                    sb.Append(".gif");                }                else if (Filename.ToLower().EndsWith("jpg"))
                    {                    sb.Append(".jpg");                }                else if (Filename.ToLower().EndsWith("jpeg"))
                    {                    sb.Append(".jpeg");                }                Filename = sb.ToString();            }            // 保存图片到服务器上            try
                {                hpf.SaveAs(Server.MapPath("\\") + "/newImage/" + Filename);                string BigPic = "/newImage/" + Filename;
                    model.ybcs_SamllPic = BigPic;            }            catch (Exception ee)
                {                Response.Write("上传图片失败,原因" + ee.Message);                return;            }            // 生成缩略图            //原始图片名称            string originalFilename = hpf.FileName;            //生成的高质量图片名称            string strFile = Server.MapPath("/newImage/Small_" + Filename);
                string small_image = "/newImage/Small_" + Filename;            model.ybcs_spPic = small_image;
                //从文件取得图片对象            System.Drawing.Image image = System.Drawing.Image.FromStream(hpf.InputStream, true);            Double Width = Double.Parse(TextBox26.Text.Trim());            Double Height = Double.Parse(TextBox27.Text.Trim());            System.Double NewWidth, NewHeight;            if (image.Width > image.Height)
                {                NewWidth = Width;                NewHeight = image.Height * (NewWidth / image.Width);            }            else
                {                NewHeight = Height;                NewWidth = (NewHeight / image.Height) * image.Width;            }            if (NewWidth > Width)
                {                NewWidth = Width;            }            if (NewHeight > Height)
                {                NewHeight = Height;            }            System.Drawing.Size size = new Size((int)NewWidth, (int)NewHeight); // 图片大小            System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width, size.Height);   //新建bmp图片            System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap);       //新建画板            graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;       //设置高质量插值法            graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;        //设置高质量,低速度呈现平滑程度            graphics.Clear(Color.White);        //清空画布            //在指定位置画图            graphics.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),                new System.Drawing.Rectangle(0, 0, image.Width, image.Height),                System.Drawing.GraphicsUnit.Pixel);            //文字水印            System.Drawing.Graphics textGraphics = System.Drawing.Graphics.FromImage(bitmap);            System.Drawing.Font font = new Font("宋体", 10);            System.Drawing.Brush brush = new SolidBrush(Color.Black);            textGraphics.DrawString(TextBox28.Text.Trim(), font, brush, 10, 10);            textGraphics.Dispose();
                try
                {                bitmap.Save(strFile, System.Drawing.Imaging.ImageFormat.Jpeg);            }            catch (Exception ex)
                {                Response.Write("保存缩略图失败:" + ex.Message);            }            graphics.Dispose();            image.Dispose();            bitmap.Dispose();      
      

  2.   

        点击上传按钮事件:
        protected void Button2_Click(object sender, EventArgs e)
        {
           string extend=this.fileup.FileName.Substring(this.fileup.FileName.LastIndexOf("."));//取出上传文件的后缀名
           if (extend.ToLower() == ".gif" || extend.ToLower() == ".jpg")
           {
               上传后的处理
           }
           else
           {
               ClientScript.RegisterStartupScript(this.GetType(),"tet","alert('请确定上传图片类型是否正确')",true);
           }    }
      

  3.   

    解决  谢谢大侠们的指点。。我是这样做的
            if (hpf.ContentType != "image/pjpeg" && hpf.ContentType != "image/gif" && hpf.ContentType != "image / jpeg") 多加了判断
      

  4.   

    现在客户端判断后缀名,然后在服务端去判断mime
      

  5.   

     if (hpf.ContentType == "image/pjpeg" || hpf.ContentType == "image/gif")
      {
          //
      }
    else{
          Response.Write("只允许上传 GIF JPG类型的文件");
          return;
    }不应该是这么写吗??
      

  6.   

    有区别么  反正到客户端都是input 服务端都是InputStream