后台代码:
1 using System;
2 using System.Configuration;
3 using System.Data;
4 using System.Linq;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.HtmlControls;
9 using System.Web.UI.WebControls;
10 using System.Web.UI.WebControls.WebParts;
11 using System.Xml.Linq;
12 using System.Data.SqlClient;
13 using System.IO;
14
15 public partial class _Default : System.Web.UI.Page
16 {
17 protected void Page_Load(object sender, EventArgs e)
18 {
19 //建立数据库连接
20 SqlConnection Con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\myasp\网站模块化开发\chpater6\App_Data\upFile_Data.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True");//本地SQL Server数据库upFile
21 String SqlCmd = "SELECT * FROM ImageStore WHERE ImageID = 1";//定义查询字符串
22 SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);//定义SqlCommand对象实例
23 Con.Open();//打开数据库连接
24
25 try
26 {
27
28 //读取数据库中的数据
29 SqlDataReader SqlReader = CmdObj.ExecuteReader();//定义 SqlDataReader对象实例
30 SqlReader.Read();//以只读方式读取数据库中的数据
31
32 //输出数据到HTTP流中
33 Response.ContentType = (string)SqlReader["ImageContentType"];//设定输出文件类型
34 Response.OutputStream.Write((byte[])SqlReader["ImageData"], 0, (int)SqlReader["ImageSize"]);//输出图象文件二进制数制
35 Response.End();//结束程序
36 Con.Close();//关闭数据库连接对象
37 }
38 catch
39 {
40 Response.End();//结束程序
41 Con.Close();//关闭数据库连接对象
42 }
43 }
44 }
45
2 using System.Configuration;
3 using System.Data;
4 using System.Linq;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.HtmlControls;
9 using System.Web.UI.WebControls;
10 using System.Web.UI.WebControls.WebParts;
11 using System.Xml.Linq;
12 using System.Data.SqlClient;
13 using System.IO;
14
15 public partial class _Default : System.Web.UI.Page
16 {
17 protected void Page_Load(object sender, EventArgs e)
18 {
19 //建立数据库连接
20 SqlConnection Con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\myasp\网站模块化开发\chpater6\App_Data\upFile_Data.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True");//本地SQL Server数据库upFile
21 String SqlCmd = "SELECT * FROM ImageStore WHERE ImageID = 1";//定义查询字符串
22 SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);//定义SqlCommand对象实例
23 Con.Open();//打开数据库连接
24
25 try
26 {
27
28 //读取数据库中的数据
29 SqlDataReader SqlReader = CmdObj.ExecuteReader();//定义 SqlDataReader对象实例
30 SqlReader.Read();//以只读方式读取数据库中的数据
31
32 //输出数据到HTTP流中
33 Response.ContentType = (string)SqlReader["ImageContentType"];//设定输出文件类型
34 Response.OutputStream.Write((byte[])SqlReader["ImageData"], 0, (int)SqlReader["ImageSize"]);//输出图象文件二进制数制
35 Response.End();//结束程序
36 Con.Close();//关闭数据库连接对象
37 }
38 catch
39 {
40 Response.End();//结束程序
41 Con.Close();//关闭数据库连接对象
42 }
43 }
44 }
45
前台代码:
默认。