1. 文件和图片,都以联接的方式给出,也就是说,文件是一个连接,图片用<img src="...">来表达,其src也是一个连接 2. 图片的部分,例程如下://show2.aspx 用来显示图片,接受参数 <%@ Page language="c#" Codebehind="Show2.aspx.cs" AutoEventWireup="false" Inherits="MedIndex.Show2" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <title>Image Show</title> <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body MS_POSITIONING="GridLayout"> <FONT face="宋体"></FONT> </body> </HTML>// show2.aspx.cs 从数据库读2进制图片文件内容 using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls;using System.Data.SqlClient; namespace MedIndex { /// <summary> /// Summary description for Show2. /// </summary> public class Show2 : System.Web.UI.Page { private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here SqlConnection myDSN = new SqlConnection(Session["DatabaseConnectionString"].ToString().Trim()); myDSN.Open(); string strCatId = Request.QueryString["CatId"]; string strCatSubId = Request.QueryString["CatSubId"]; string strCatSubPrjId = Request.QueryString["CatSubPrjId"]; string sqlText = "SELECT CatSubPrjImage, CatSubPrjImageType FROM tMedIndexCatSubPrj" + " WHERE CatId = '" + strCatId + "' AND CatSubId = '" + strCatSubId + "' AND CatSubPrjId = '" + strCatSubPrjId + "'"; SqlCommand MyCommand = new SqlCommand (sqlText, myDSN); SqlDataReader dr = MyCommand.ExecuteReader(); if(dr.Read()) { Response.ContentType = (dr["CatSubPrjImageType"].ToString()); Response.BinaryWrite((byte[])dr["CatSubPrjImage"]); } myDSN.Close(); } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); }
/// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion } }
2. 图片的部分,例程如下://show2.aspx 用来显示图片,接受参数
<%@ Page language="c#" Codebehind="Show2.aspx.cs" AutoEventWireup="false" Inherits="MedIndex.Show2" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Image Show</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<FONT face="宋体"></FONT>
</body>
</HTML>// show2.aspx.cs 从数据库读2进制图片文件内容
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;using System.Data.SqlClient;
namespace MedIndex
{
/// <summary>
/// Summary description for Show2.
/// </summary>
public class Show2 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
SqlConnection myDSN = new SqlConnection(Session["DatabaseConnectionString"].ToString().Trim());
myDSN.Open(); string strCatId = Request.QueryString["CatId"];
string strCatSubId = Request.QueryString["CatSubId"];
string strCatSubPrjId = Request.QueryString["CatSubPrjId"]; string sqlText = "SELECT CatSubPrjImage, CatSubPrjImageType FROM tMedIndexCatSubPrj"
+ " WHERE CatId = '" + strCatId + "' AND CatSubId = '" + strCatSubId + "' AND CatSubPrjId = '" + strCatSubPrjId + "'";
SqlCommand MyCommand = new SqlCommand (sqlText, myDSN);
SqlDataReader dr = MyCommand.ExecuteReader();
if(dr.Read())
{
Response.ContentType = (dr["CatSubPrjImageType"].ToString());
Response.BinaryWrite((byte[])dr["CatSubPrjImage"]);
}
myDSN.Close();
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
Response.BinaryWrite((byte[])dr["CatSubPrjImage"]);第一句声明这个页面输出的类型,是txt/html,还是gif
第二句把数据库中读到的数据写到流里
http://www.codeproject.com/aspnet/ImageBinaryStream.asp