限制圖片
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;
}
}
}
{
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;
}
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.