限制图片

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;
  }
  }
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值