文件或图片上传 asp.net图片上传 文件上传 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 将图像存储到数据库中//上传图片到数据库// 上传图片 protected void imgBtnLoad_Click(object sender, ImageClickEventArgs e) { if (!fulPhoto.HasFile) { lbMessage.Text = "请选择上传图片!"; return; } else { try { //获取上传文件路径 string filePath = fulPhoto.PostedFile.FileName; //获取上传文件后缀 string fileExt = filePath.Substring(filePath.LastIndexOf(".") + 1); //限定上传格式 if (fileExt.ToLower() == "gif" || fileExt.ToLower() == "jpg" || fileExt.ToLower() == "bmp" || fileExt.ToLower() == "png") { if (fulPhoto.PostedFile.ContentLength > 5120000) { lbMessage.Text = "限定上传图片的大小不能超出5M!"; return; }//CodeGo.net/ else { //根据时间生成文件名 string nowTime = Album.CreateDateTimeString(); string fileName = nowTime + "." + fileExt; //源文件保存路径 string savePath = Server.MapPath("UpFile/"); //缩略图保存路径 string imgPath = Server.MapPath("UpSmall/"); //上传图片 fulPhoto.PostedFile.SaveAs(savePath + fileName); System.Drawing.Image img = System.Drawing.Image.FromFile(savePath + fileName); if (img.Width >= img.Height) { am.GetThumbnail(savePath + fileName, imgPath + fileName, 400, 300, "Cut"); } else { am.GetThumbnail(savePath + fileName, imgPath + fileName, 320, 350, "Cut"); } //文件类型 string p_type = fulPhoto.PostedFile.ContentType; //文件大小 int p_size = fulPhoto.PostedFile.ContentLength; int categoryId = Convert.ToInt32(ddlCategory.SelectedValue); //调用类方法将数据插入到数据库 int result = am.AddPhoto(tbName.Text.Trim(), tbDescript.Text.Trim(), fileName, p_type, p_size, categoryId); ScriptManager.RegisterStartupScript(UpdatePanel1, typeof(UpdatePanel), "scriptname", "alert('图片上传成功!!');", true); } } else { lbMessage.Text = "只允许上传gif,jpg,bmp,png格式的图片文件!"; return; } } catch (Exception ex) { throw new Exception(ex.Message, ex); } }}//读取数据库图片显示图片名称 public void BindDataList(int currentpage) { SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["conStr"]); pds.AllowPaging = true;//允许分页 pds.PageSize = 6;//每页显示3条数据 pds.CurrentPageIndex = currentpage;//当前页为传入的一个int型值 string strSql = "select * from PerHomeDetail order by PerHomeId desc"; conn.Open();//打开数据库连接 SqlDataAdapter sda = new SqlDataAdapter(strSql, conn); DataSet ds = new DataSet(); sda.Fill(ds);//把执行得到的数据放在数据集中 pds.DataSource = ds.Tables[0].DefaultView;//把数据集中的数据放入分页数据源中 dlPictrue.DataSource = pds;//把数据集中的数据放入分页数据源中 dlPictrue.DataBind();//绑定数据 conn.Close();//关于点击图片放大示例非常简单这里字符关系(省略) } http://www.cnblogs.com/qiantuwuliang/archive/2009/08/21/1551200.html SQL Sever数据库的连接问题 ajaxpro级联问题 一个关于javascript弹出窗口在ie6和ie7上运行的问题? 关于asp.net中页面间传值问题。急!!! 再次求助,高手帮我啊》》 一个正则表达式如果有回车时怎样处理? 我是ASP初学者,急需高人帮我断下代码.这个代码从上数第几行开始截断就是"公司名称搜索""公司网址"等搜索部分了? 日期显示为什么不对? 关于vs.net的小问题! ASP.net 中如何生成uniqueidentifier列的新值? AJAX 以json格式传值在后台得不到值 新手请教。 【请教】如何修改 快捷方式 文件的目标地址?
//上传图片到数据库
// 上传图片
protected void imgBtnLoad_Click(object sender, ImageClickEventArgs e)
{
if (!fulPhoto.HasFile)
{
lbMessage.Text = "请选择上传图片!";
return;
}
else
{
try
{
//获取上传文件路径
string filePath = fulPhoto.PostedFile.FileName;
//获取上传文件后缀
string fileExt = filePath.Substring(filePath.LastIndexOf(".") + 1);
//限定上传格式
if (fileExt.ToLower() == "gif" || fileExt.ToLower() == "jpg" || fileExt.ToLower() == "bmp" || fileExt.ToLower() == "png")
{
if (fulPhoto.PostedFile.ContentLength > 5120000)
{
lbMessage.Text = "限定上传图片的大小不能超出5M!";
return;
}//CodeGo.net/
else
{
//根据时间生成文件名
string nowTime = Album.CreateDateTimeString();
string fileName = nowTime + "." + fileExt;
//源文件保存路径
string savePath = Server.MapPath("UpFile/");
//缩略图保存路径
string imgPath = Server.MapPath("UpSmall/");
//上传图片
fulPhoto.PostedFile.SaveAs(savePath + fileName);
System.Drawing.Image img = System.Drawing.Image.FromFile(savePath + fileName);
if (img.Width >= img.Height)
{
am.GetThumbnail(savePath + fileName, imgPath + fileName, 400, 300, "Cut"); }
else
{
am.GetThumbnail(savePath + fileName, imgPath + fileName, 320, 350, "Cut");
}
//文件类型
string p_type = fulPhoto.PostedFile.ContentType;
//文件大小
int p_size = fulPhoto.PostedFile.ContentLength;
int categoryId = Convert.ToInt32(ddlCategory.SelectedValue);
//调用类方法将数据插入到数据库
int result = am.AddPhoto(tbName.Text.Trim(), tbDescript.Text.Trim(), fileName, p_type, p_size, categoryId);
ScriptManager.RegisterStartupScript(UpdatePanel1, typeof(UpdatePanel), "scriptname", "alert('图片上传成功!!');", true);
}
}
else
{
lbMessage.Text = "只允许上传gif,jpg,bmp,png格式的图片文件!";
return;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
}
//读取数据库图片显示图片名称
public void BindDataList(int currentpage)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["conStr"]);
pds.AllowPaging = true;//允许分页
pds.PageSize = 6;//每页显示3条数据
pds.CurrentPageIndex = currentpage;//当前页为传入的一个int型值
string strSql = "select * from PerHomeDetail order by PerHomeId desc"; conn.Open();//打开数据库连接
SqlDataAdapter sda = new SqlDataAdapter(strSql, conn);
DataSet ds = new DataSet();
sda.Fill(ds);//把执行得到的数据放在数据集中
pds.DataSource = ds.Tables[0].DefaultView;//把数据集中的数据放入分页数据源中
dlPictrue.DataSource = pds;//把数据集中的数据放入分页数据源中
dlPictrue.DataBind();//绑定数据
conn.Close();//关于点击图片放大示例非常简单这里字符关系(省略)
}