2024年5月30日发(作者:)
有一个员工表Employee,需要保存员工照片(Photo)到数据库(sql server)上。员工照片
对应的字段是varbinary(max),也就是要存成二进制文件类型(这和以前讨巧地存图片文件路
径就不相同了),默认可以为空。下面说说主要实现思路:
1、存取图片
(1)、将图片文件转换为二进制并直接存进sql server
//
///
/// 将图片转化为长二进制
///
/// "photopath">
///
public static Byte[] SetImgToByte(string imgPath)
{
FileStream file = new FileStream(imgPath, , );
Byte[] byteData = new Byte[];
(byteData, 0, );
();
return byteData;
}
///
/// 将转换成二进制码的图片保存到数据库中
///
public static bool SaveEmployeeImg2Db(Employee model, string path)
{
try
{
Byte[] imgBytes = SetImgToByte(path);
= imgBytes;
bool flag=ployeePhoto(model); //EmployeeService是公
司内部的库调用,插入或者更新照片,这里不透露细节
return flag;
}
catch (Exception ex)
{
throw ex;
}
}
(2)、在网页中上传图片
///
/// 上传图片
///
/// "sender">
/// "e">
protected void btnUpload_Click(object sender, EventArgs e)
{
string serverPath = h("~/images/");
if (e) //fuPhoto是fileupload控件
{
string fileName = me;
FileInfo fi = new FileInfo(fileName);
string mimeType = r();
if (f("image") < 0)
{
//("上传的照片格式不对");
}
else if( > 2* 1024 * 1024)


发布评论