限制图片

protected void ibtnAdd_Click(object sender, ImageClickEventArgs e)
  {
  string strProductPic = "", filepath = "";
  if (FupFoodPic.HasFile == true)
  {
  strProductPic = this.FupFoodPic.FileName;
  string strPicPath = FupFoodPic.PostedFile.FileName;
  string type = strProductPic.Substring(strProductPic.LastIndexOf(".") + 1).ToLower();//获取文件的类型

  //限制图片的分辨率
  System.Drawing. Image img = System.Drawing. Image.FromStream(FupFoodPic.PostedFile.InputStream);
  int width = img.Width;
  int heigth = img.Height;
  if (width > 136 && heigth > 136)
  {
  Confirm("图片分辨率仅限于136*136以下!", "btnUpPicture", this.Page);
  return;
  }

  if (type != "jpg" & type != "jpeg" & type != "bmp" & type != "gif" & type != "png")
  {
  Confirm("上传失败,格式错误,仅支持jpg,gif,png,bmp图片格式!", "btnUpPicture", this.Page);
  return;
  }

  //限制相片大小
  else if (FupFoodPic.PostedFile.ContentLength > 256000)
  {
  Confirm("图片大小超出范围,上传失败,仅限于256KB以下!", "ibtnAdd", this.Page);
  return;
  }

  filepath = MapPath("../img/Food/" + DateTime.Now.ToString("yyyyMMddHHmmss") + "." + type);
  if (!File.Exists(filepath))
  {
  this.FupFoodPic.SaveAs(filepath);//图片保存服务器磁盘

  //数据保存在数据库  
  food.FoodName = this.txtFoodName.Text.Trim();
  food.FoodPic = "../img/Food/" + DateTime.Now.ToString("yyyyMMddHHmmss") + "." + type;//相片路径保存到数据库
  food.FoodPicName = DateTime.Now.ToString("yyyyMMddHHmmss") + "." + type;//图片名称
  food.FoodMaterial = this.txtFoodyuanliao.Text.Trim();
  food.FoodDo = this.txtFoodDo.Text.Trim();
  food.FoodNutrition = this.txtJiaZhi.Text.Trim();
  food.AddTime = DateTime.Now.ToString("yyyy年MM月dd日HH时mm分ss秒");//添加时间  

  int foodId = TipsManager.AddFoods(food);
  if (foodId > 0)
  {
  Confirm("数据添加成功!", "ibtnAdd", this.Page);
  ClearText();
  DataBind();
  this.lblTotal.Text = "第1页/共" + (gvView.PageCount) + "页";
  return;
  }
  else
  {
  Confirm("数据添加失败!", "ibtnAdd", this.Page);
  ClearText();
  return;
  }
  }
  else
  {
  Confirm("上传失败,同名相片存在!", "ibtnAdd", this.Page);
  return;
  }
  }
  }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章