如何把Image控件显示的图片存储到数据库中? 我现在用image控件显示图片,想把image控件显示的图片存储到数据库中,请问应该怎么做?请指导一下,最好能给点代码! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 获取到image控件中图片的地址1.根据地址把图片保存在你的服务器上 再把图片的地址保存在数据库中2.根据图片的地址用2进制的方式写入到数据库中去 有两种方法1. 把文件路径保存到数据库中,在Page_Load中打工数据库,把路径赋给image。注意要用想对路径,因为服务器和本地的绝对路径是不一样的。2. 把数据直接存到数据库中,用流的方法存取。 //将图片以二进制上传到数据库 HttpPostedFile upPhoto = FileUpload1.PostedFile; int upPhotoLength = upPhoto.ContentLength; byte[] PhotoArray = new Byte[upPhotoLength]; Stream PhotoStream = upPhoto.InputStream; PhotoStream.Read(PhotoArray, 0, upPhotoLength); string sql = "insert into PostImg values(@name,@img)"; SqlParameter[] parameters ={ new SqlParameter("@name",SqlDbType.NVarChar), new SqlParameter("@img",SqlDbType.Image) }; parameters[0].Value = FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.LastIndexOf("\\") + 1, FileUpload1.PostedFile.FileName.Length - FileUpload1.PostedFile.FileName.LastIndexOf("\\") - 1); parameters[1].Value = PhotoArray; DBHelper.ExecuteCommand(sql, parameters); Page.ClientScript.RegisterStartupScript(typeof(string), "srcc", "<script>alert('上传成功!')</script>"); //上传路径到数据库 protected void Button1_Click(object sender, EventArgs e) { try { string strFileName = this.FileUpload1.PostedFile.FileName; int nLength = strFileName.Length - strFileName.LastIndexOf("\\") - 1; strFileName = strFileName.Substring(strFileName.LastIndexOf("\\") + 1, nLength); string strPath = Server.MapPath("Upload\\"); string type = strFileName.Substring(strFileName.LastIndexOf(".") + 1);//得到文件的后缀名 if (!(type == "jpg" || type == "bmp" || type == "gif" || type == "ico"||type=="txt")) { Response.Write("<script language='javascript'>alert('你上传的图片格式有错误!')</script>"); return; } FileUpload1.PostedFile.SaveAs(strPath + strFileName); this.labmessage.Text = "上传成功"; labmessage.Text = Application["peoplenumber"].ToString(); PostUrl(strFileName,strPath); 大家帮我参考下. 一个关天ASP.NET页面设计的问题 求助,在线等,关于freeTextBox中提交时候的编译错误问题 在线等!!!怎么使GridView中的部分行,以粗体显示 application_end()事件怎么在服务器上不执行? 创建Message Queue时Transactional 选项的作用?(帮顶有分) javascript访问web service的问题 谁做过icbc的b2b开发 那里有ASP.NET下的WEB控件下载(如TreeView,ToolBar,DataGrid) 十万火急 对asp.net的一点疑问! 怎样对中文进行解密
1.根据地址把图片保存在你的服务器上 再把图片的地址保存在数据库中
2.根据图片的地址用2进制的方式写入到数据库中去
HttpPostedFile upPhoto = FileUpload1.PostedFile;
int upPhotoLength = upPhoto.ContentLength;
byte[] PhotoArray = new Byte[upPhotoLength];
Stream PhotoStream = upPhoto.InputStream;
PhotoStream.Read(PhotoArray, 0, upPhotoLength);
string sql = "insert into PostImg values(@name,@img)";
SqlParameter[] parameters ={
new SqlParameter("@name",SqlDbType.NVarChar),
new SqlParameter("@img",SqlDbType.Image)
};
parameters[0].Value = FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.LastIndexOf("\\") + 1, FileUpload1.PostedFile.FileName.Length - FileUpload1.PostedFile.FileName.LastIndexOf("\\") - 1);
parameters[1].Value = PhotoArray;
DBHelper.ExecuteCommand(sql, parameters);
Page.ClientScript.RegisterStartupScript(typeof(string), "srcc", "<script>alert('上传成功!')</script>");
protected void Button1_Click(object sender, EventArgs e)
{
try
{
string strFileName = this.FileUpload1.PostedFile.FileName;
int nLength = strFileName.Length - strFileName.LastIndexOf("\\") - 1;
strFileName = strFileName.Substring(strFileName.LastIndexOf("\\") + 1, nLength);
string strPath = Server.MapPath("Upload\\");
string type = strFileName.Substring(strFileName.LastIndexOf(".") + 1);//得到文件的后缀名
if (!(type == "jpg" || type == "bmp" || type == "gif" || type == "ico"||type=="txt"))
{
Response.Write("<script language='javascript'>alert('你上传的图片格式有错误!')</script>");
return;
}
FileUpload1.PostedFile.SaveAs(strPath + strFileName);
this.labmessage.Text = "上传成功";
labmessage.Text = Application["peoplenumber"].ToString();
PostUrl(strFileName,strPath);