在sqlserver中的图片类型是image 然后,通过dataset保存到数据库中,通过showimg.aspx文件来读出图片,即显示图片,代码如下: Dim image As Byte() = IssueQuestionRow.QuestionImage '/转换为支持存储区为内存的流 Dim memStream As New System.IO.MemoryStream(image) '/定义并实例化Bitmap对象 Dim bm As New Bitmap(memStream) '/根据不同的条件进行输出或者下载; Response.Clear() '/如果请求字符串指定下载,就下载该文件; '/否则,就显示在浏览器中。 If Request.QueryString("Download") = "1" Then Response.Buffer = True Response.ContentType = "application/octet-stream" '/这里下载输出的文件名字 ok.jpg 为例子,你实际中可以根据情况动态决定。 Response.AddHeader("Content-Disposition", "attachment;filename=ok.jpg") Else Response.ContentType = "image/jpg" End If Response.BinaryWrite(image) Response.End() 然后通过需要调用显示图片的页面,加入 <img src=”./showimg.aspx” wigth=”100px” height=”50”> 来固定图片的显示位置、大小等。 当然也可以通过一个页面的不同参数来获得不同的图片,如下代码: Showimg.aspx文件: Public QuestionID As String Public ChapterID As String Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '在此处放置初始化页的用户代码 If Not IsPostBack Then QuestionID = Request.QueryString("QID") ChapterID = Request.QueryString("ChapterID") Exercise = EXH.GetExercise(ChapterID) Dim dv As New DataView(Exercise.Ex_IssueQuestion) dv.RowFilter = "QuestionID='" + QuestionID + "'" If dv.Count > 0 Then IssueQuestionRow = dv.Item(0).Row Dim image As Byte() = IssueQuestionRow.QuestionImage '/转换为支持存储区为内存的流 Dim memStream As New System.IO.MemoryStream(image) '/定义并实例化Bitmap对象 Dim bm As New Bitmap(memStream) '/根据不同的条件进行输出或者下载; Response.BinaryWrite(image) End If End If End Sub在其他需要调用的地方的aspx页面里只需写:<img src=”./showimg.aspx?QuestionID=222&ChapterID=3” wigth=”100px” height=”50”>即可
例子
//上传文件
public void FN_UpFiles()
{
//遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
//StringBuilder strMsg = new StringBuilder();
//strMsg.Append("上传的文件分别是:<hr color='pink'/>");
try
{
for (int iFile = 0 ; iFile < files.Count ; iFile++)
{
//检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName = "";
//string fileExtension = "";
fileName = Path.GetFileName(postedFile.FileName);
if (fileName != "")
{
try
{
string strpath = HttpContext.Current.Request.MapPath("~/ResourcesFolder/") + fileName;
if (System.IO.File.Exists(strpath))
{
Response.Write("已经存在文件:" + fileName + "<br>");
}
else
{
try
{
NRModel.File model = new NRModel.File();
NRBLL.File bf = new NRBLL.File();
Guid guid1 = Guid.NewGuid();
Guid guid2 = Guid.NewGuid();
Guid guid3 = Guid.NewGuid();
Guid guid4 = Guid.NewGuid();
model.Fileid = guid1;
model.Folderid = guid2;
model.Filepath = strpath;
model.FileNam = fileName.ToString();
model.FileSize = postedFile.ContentLength;
model.Decription = this.decrition.Value;
model.CreateOn = DateTime.Now;
model.CreateBy = guid3;
model.ModefyBy = guid4;
if (bf.FN_AddNewRes(model) > 0)
{
//fileExtension = Path.GetExtension(fileName);
//strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
//strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
//strMsg.Append("上传文件的文件名:" + fileName + "<br>");
//strMsg.Append("上传文件的扩展名:" + fileExtension + "<br>");
//strMsg.Append("上传文件的大小:" + postedFile.ContentLength.ToString() + "个字节" + "<br>");
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("~/ResourcesFolder/") + fileName);
Page.RegisterStartupScript("提示", "<script language='javascript'>alert('上传成功!')</script>");
Response.Write("<script language='javascript'>self.opener.location.reload();</script>");
Response.Write("<script language='javascript'>window.close();</script>");
}
}
catch (Exception ex)
{ Response.Write(ex.ToString());
} } }
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
else
{
Page.RegisterStartupScript("提示", "<script language='javascript'>alert('没有添加上传文件!')</script>");
}
}
//strStatus.Text = strMsg.ToString();
}
catch (System.Exception ex)
{
Response.Write(ex.ToString());
}
}
byte[] vals = new byte[(int)fileStream.Length];
fileStream.Read(vals, 0, vals.Length);
INSERT INTO Files vals(vals)
二进制图片显示上传二进制
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();
}
}
}读取的时候
<img src="a.aspx?id=x" />
show.aspx 的后台
private void Page_Load(object sender, System.EventArgs e)
{
string strID = Request.QueryString["id"];
string sqlstr = "select photo from Tb where id=@id";
SqlConnection myConnection = new SqlConnection(connstr);
SqlCommand myCommand = new SqlCommand(sqlstr, myConnection);
myCommand.AddWithValue("@id", strID);
try
{
myConnection.Open();
SqlDataReader dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
if (dr.Read())
{
Response.Clear();
Response.ContentType = "image/gif";
Response.BinaryWrite((byte[])dr["photo"]);
}
myConnection.Close();
}
catch (SqlException ex)
{
}
Response.End();
}
然后,通过dataset保存到数据库中,通过showimg.aspx文件来读出图片,即显示图片,代码如下:
Dim image As Byte() = IssueQuestionRow.QuestionImage
'/转换为支持存储区为内存的流
Dim memStream As New System.IO.MemoryStream(image)
'/定义并实例化Bitmap对象
Dim bm As New Bitmap(memStream)
'/根据不同的条件进行输出或者下载;
Response.Clear()
'/如果请求字符串指定下载,就下载该文件;
'/否则,就显示在浏览器中。
If Request.QueryString("Download") = "1" Then
Response.Buffer = True
Response.ContentType = "application/octet-stream"
'/这里下载输出的文件名字 ok.jpg 为例子,你实际中可以根据情况动态决定。
Response.AddHeader("Content-Disposition", "attachment;filename=ok.jpg")
Else Response.ContentType = "image/jpg"
End If
Response.BinaryWrite(image) Response.End()
然后通过需要调用显示图片的页面,加入 <img src=”./showimg.aspx” wigth=”100px” height=”50”>
来固定图片的显示位置、大小等。
当然也可以通过一个页面的不同参数来获得不同的图片,如下代码:
Showimg.aspx文件:
Public QuestionID As String
Public ChapterID As String
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
If Not IsPostBack Then
QuestionID = Request.QueryString("QID")
ChapterID = Request.QueryString("ChapterID")
Exercise = EXH.GetExercise(ChapterID)
Dim dv As New DataView(Exercise.Ex_IssueQuestion)
dv.RowFilter = "QuestionID='" + QuestionID + "'"
If dv.Count > 0 Then
IssueQuestionRow = dv.Item(0).Row
Dim image As Byte() = IssueQuestionRow.QuestionImage
'/转换为支持存储区为内存的流
Dim memStream As New System.IO.MemoryStream(image)
'/定义并实例化Bitmap对象
Dim bm As New Bitmap(memStream)
'/根据不同的条件进行输出或者下载; Response.BinaryWrite(image)
End If
End If
End Sub在其他需要调用的地方的aspx页面里只需写:<img src=”./showimg.aspx?QuestionID=222&ChapterID=3” wigth=”100px” height=”50”>即可