将图片存入数据库中,大家进来看下,谢谢
是这样的!!
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DirectoryInfo di = new DirectoryInfo(@"D:\ASPPPPPPPPPPPPPP\CSDNCSDN例子\7-12\images");
FileInfo[] fimore = di.GetFiles();
foreach (FileInfo fi in fimore)
{
ListItem li = new ListItem(Path.GetFileNameWithoutExtension(fi.FullName), Path.GetFileName(fi.FullName));
DropDownList1.Items.Add(li);
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Image1.ImageUrl = "~/images/"+DropDownList1.SelectedItem.Value;
}
protected void Button1_Click(object sender, EventArgs e)
{
}
现在想实现按钮的功能,将图片存入数据库中,
数据库的字段有
Name(varchar 50) ,Img(image),Path(255),Type(255)
网上找过例子了,但不尽人意!希望大家帮帮忙! --------------------编程问答-------------------- upup --------------------编程问答-------------------- byte[] imgBt = new byte[imgUpload.ContentLength];
imgUpload.InputStream.Read(imgBt, 0, imgUpload.ContentLength);
c.Logo = imgBt;
CompanyRepository.UpdateCompanyLogo(c);
存图片的代码 --------------------编程问答-------------------- 一般的做法不会把图片保存到数据库,而是上传到服务器配置好的路径下,数据库存的只是图片的路径,访问的时候,指向存储路径就好了。 --------------------编程问答--------------------
不是能转化成二进制,然后存入数据库中的么 --------------------编程问答-------------------- 图片 还是放在磁盘上面好 --------------------编程问答-------------------- 图片都放数据库么。有必要么。 --------------------编程问答-------------------- 感觉很蛋痛,为什么存数据库了,如果人家高清图片你存也慢,人家访问也慢,用户多点更加慢! --------------------编程问答-------------------- 我顶,放二进制太麻烦,把文件放到到磁盘中为好 --------------------编程问答-------------------- function ShowMoney(obj)
{
var count= $("#"+obj.id).val();
var price =$("#"+obj.id.replace("txtcount","lblprice")).val();
var sum=parseInt(count)*parseInt(price);
$("#"+obj.id.replace("txtcount","txtsum")).val(price);
}
--------------------编程问答-------------------- 不好意思发错了! --------------------编程问答--------------------
+ --------------------编程问答-------------------- 图片存数据库挺简单的,就是保存二进制流吗,你哪里不会 --------------------编程问答-------------------- 路径不对。
二进制也可以
二进制图片显示
上传二进制
if (FileUpLogo.HasFile)
{
//取得上传文件的大小
int FileLen = FileUpLogo.PostedFile.ContentLength;
Byte[] FileData = new Byte[FileLen];
//创建访问客户端上传文件的对象
HttpPostedFile hp = FileUpLogo.PostedFile;
//创建数据流对象
System.IO.Stream sr = hp.InputStream;
//将图片数据放到FileData数组对象实例中,0代表数组指针的起始位置,FileLen代表指针的结束位置
sr.Read(FileData, 0, FileLen);
//将FileData 赋值给实体
brandModel.fld_logo = FileData;
}
或者
HttpPostedFile upFile = up_file.PostedFile;//HttpPostedFile对象,用来读取上传图片的属性
fileLength = upFile.ContentLength;//记录文件的长度
try
{
if(fileLength==0)//当文件长度为0的时候
{
txtMessage.Text = "请选择要上传的文件!";
}
else
{
byte[] fileByte = new byte[fileLength];//用图片的长度来初始化一个字节数组存储临时的图片文件
Stream fileStream = upFile.InputStream;//建立文件流对象
fileStream.Read(fileByte,0,fileLength);//读取图片数据到临时存储体fileByte,0为数据指针位置,fileLength为数据长度
string connString = "Data Source=192.168.1.250;database=image;uid=pwqzc;pwd=cn0088";
SqlConnection conn = new SqlConnection(connString);//初始化数据库连接
string insertStr = "insert into image (image_data,image_content_type,image_description,image_size) values (@image_data,@image_content_type,@image_description,@image_size)";
//插入数据库语句
SqlCommand comm = new SqlCommand(insertStr,conn);
comm.Parameters.Add(new SqlParameter("@image_data",SqlDbType.Image));//添加参数
comm.Parameters["@image_data"].Value = fileByte;//给参数赋值
comm.Parameters.Add(new SqlParameter("@image_content_type",SqlDbType.VarChar,50));
comm.Parameters["@image_content_type"].Value = upFile.ContentType;//记录图片类型
comm.Parameters.Add(new SqlParameter("@image_description",SqlDbType.VarChar,50));
comm.Parameters["@image_description"].Value = txtDescription.Text;//把其他的表单数据上传
comm.Parameters.Add(new SqlParameter("@image_size",SqlDbType.Int,4));
comm.Parameters["@image_size"].Value = upFile.ContentLength;//记录图片长度,读取数据的时候使用
conn.Open();//打开数据库连接
comm.ExecuteNonQuery();//添加数据
conn.Close();//关闭数据库
txtMessage.Text = "你已经成功的上传了图片";
}
}
catch(Exception ex)
{
txtMessage.Text = ex.Message.ToString();
}
}
}
读取
using(SqlConnection conn=new SqlConnection())
{
conn.ConnectionString="";
string strSql="select * from Tb where Id='"+Id+"'";
SqlCommand cmd=new SqlCommand(strSql,conn) ;
conn.Open();
SqlDataReader reader=cmd.ExecuteReader();
if(reader.Read())
{
Response.ContentType = "application/octet-stream";
Response.BinaryWrite((Byte[])reader["Photo"]);
}
Response.End();
conn.Close();
}
补充:.NET技术 , ASP.NET