上传图片问题
再。net三层架构中怎么将图片保存到数据库中,求在每一层中的代码,谢谢了 --------------------编程问答-------------------- 将图片转化成二进制后保存到DB中 --------------------编程问答-------------------- 数据库字段为image类型HttpPostedFile UpFile = UP_FILE.PostedFile; //HttpPostedFile对象,用于读取图象文件属性
FileLength = UpFile.ContentLength; //记录文件长度
try {
if (FileLength == 0) { //文件长度为零时
txtMessage.Text = "<b>请你选择你要上传的文件</b>";
} else {
Byte[] FileByteArray = new Byte[FileLength]; //图象文件临时储存Byte数组
Stream StreamObject = UpFile.InputStream; //建立数据流对像
//读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度
StreamObject.Read(FileByteArray,0,FileLength);
//建立SQL Server链接
SqlConnection Con = new SqlConnection("Data Source=Localhost;Initial Catalog=testdb;User ID=sa;Pwd=;");
String SqlCmd = "INSERT INTO ImageStore (ImageData, ImageContentType, ImageDescription, ImageSize) VALUES (@Image, @ContentType, @ImageDescription, @ImageSize)";
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
CmdObj.Parameters.Add("@Image",SqlDbType.Binary, FileLength).Value = FileByteArray;
CmdObj.Parameters.Add("@ContentType", SqlDbType.VarChar,50).Value = UpFile.ContentType; //记录文件类型
//把其它单表数据记录上传
CmdObj.Parameters.Add("@ImageDescription", SqlDbType.VarChar,200).Value = txtDescription.Text;
//记录文件长度,读取时使用
CmdObj.Parameters.Add("@ImageSize", SqlDbType.BigInt,8).Value = UpFile.ContentLength;
Con.Open();
CmdObj.ExecuteNonQuery();
Con.Close();
txtMessage.Text = "<p><b>OK!你已经成功上传你的图片</b>";//提示上传成功
}
} catch (Exception ex) {
txtMessage.Text = ex.Message.ToString();
}}} --------------------编程问答-------------------- 每一层代码? 其实很简单,直接在数据库中保持图片的路径就可以了,这没什么难的, --------------------编程问答-------------------- 图片我都是保存路径。。。 --------------------编程问答-------------------- 上传代码一般写到公共类,返回上传的文件名,赋值给页面后台的model跟层没多大关系 --------------------编程问答-------------------- public class RecrultmentBLL
{
public void AddBlog(params string[] userInfo)
{
StringBuilder sqlInsertBlog = new StringBuilder();
sqlInsertBlog.Append("insert into ");
sqlInsertBlog.Append(BlogData.Blog_TABLE);
sqlInsertBlog.Append(" ( ");
sqlInsertBlog.Append(BlogData.Name_FIELD);
sqlInsertBlog.Append(",");
sqlInsertBlog.Append(BlogData.NickName_FIELD);
sqlInsertBlog.Append(",");
sqlInsertBlog.Append(BlogData.Pic_FIELD);
sqlInsertBlog.Append(",");
sqlInsertBlog.Append(BlogData.Description_FIELD);
sqlInsertBlog.Append(",");
sqlInsertBlog.Append(BlogData.Announcement_FIELD);
sqlInsertBlog.Append(" ) ");
sqlInsertBlog.Append(" values ");
sqlInsertBlog.Append("(");
sqlInsertBlog.Append("@Name,@NickName,@Pic,@Description,@Announcement)");
//新建Blog表参数对象
string BlogId = TopGunCommon.GetNewID(BlogData.Blog_TABLE);
SqlParameter sqlparmName = DataHelper.CreateInputParam("@Name", SqlDbType.Text, userInfo[0]);
SqlParameter sqlparmNickName = DataHelper.CreateInputParam("@NickName", SqlDbType.Text, userInfo[1]);
SqlParameter sqlparmPic = DataHelper.CreateInputParam("@Pic", SqlDbType.Image, userInfo[2]);
SqlParameter sqlparmDescription = DataHelper.CreateInputParam("@Description", SqlDbType.Text, userInfo[3]);
SqlParameter sqlparmAnnouncement = DataHelper.CreateInputParam("@Announcement", SqlDbType.Text, userInfo[4]);
//获得一个事务
SqlServerTransaction trans = SqlCommon.NewTransaction;
//创建执行对象
DataHelper DHUser = new DataHelper(trans);
DataHelper DHUserBase = new DataHelper(trans);
//执行操作
try
{
DHUser.ExecCommand(sqlInsertBlog.ToString(), sqlparmName, sqlparmNickName, sqlparmPic,sqlparmDescription, sqlparmAnnouncement);
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
}
}
}
这是我BLL层里面的代码,请问在WEBUI层里面怎么让图片传到这里面来 --------------------编程问答-------------------- Byte[]
传这个啊 --------------------编程问答-------------------- 图片路径保存在数据库中.就可了
补充:.NET技术 , ASP.NET