62,074
社区成员
发帖
与我相关
我的任务
分享
int id = Convert.ToInt16(Request.QueryString["id"]); //把图片的ID转换成INT型的
DataSetDAL.photoDataTable _photoDataTable = _photoTableAdapter.GetDataById(id); //实例化一个类
DataSetDAL.photoRow _photoRow = _photoDataTable[0];
if (!_photoRow.Isp_sImgByteNull())
{
Response.BinaryWrite(_photoRow.p_sImgByte); //关键是这句显示图片的
}
if (Request.QueryString["id"] != null)
{
string path = Server.MapPath("images/" + Request.QueryString["id"] + ".jpg");
FileStream fs = new FileStream(path, FileMode.Open);
byte[] b = new byte[fs.Length];
fs.Read(b, 0, b.Length);
fs.Close();
fs.Dispose();
//上面换成你从数据库中读取出来的二进制数据
MemoryStream ms = new MemoryStream(b);
Response.BinaryWrite(ms.ToArray());
Response.End();
}
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();
}