以下是存图片的例子, 同样适合存 word文件
ContentType里记录了文件的类型, word是 application/msword, jpg图片是image/jpeg<%@Page language="C#" %>
<%@import namespace="System.IO"%>
<%@import namespace="System.Data"%>
<%@import namespace="System.Data.SqlClient"%>
<script language="C#" runat="server">
public void Button_Submit(Object o, EventArgs e)
{
HttpPostedFile upFile = up_file.PostedFile;
int iFileLength = upFile.ContentLength;
try
{
if(iFileLength == 0)
{
txtMess.Text = "请选择要上传的文件!";
}
else
{
Byte[] FileByteArray = new Byte[iFileLength];
Stream StreamObject = upFile.InputStream;
StreamObject.Read(FileByteArray, 0, iFileLength);
SqlConnection conn = new SqlConnection("server=yy;uid=sa;pwd=;database=pany");
string sql = "insert into t_imgs (imgData, type, description, imgSize) values " 
+ "(@Image, @ContentType, @ImageDescription, @ImgSize)";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add("@Image", SqlDbType.Binary, iFileLength).Value = FileByteArray;
cmd.Parameters.Add("@ContentType", SqlDbType.VarChar, 50).Value = upFile.ContentType;
cmd.Parameters.Add("@ImageDescription", SqlDbType.VarChar, 200).Value = txtDesc.Text;
cmd.Parameters.Add("@ImgSize", SqlDbType.BigInt, 8).Value = upFile.ContentLength;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
txtDesc.Text = "";
txtMess.Text = "OK!你已经成功上传了类型的文件";
}
}
catch(Exception ex)
{
txtMess.Text = ex.Message.ToString();
}
}

</script><html>
<head>
<title>上传图片</title>
</head>
<body bgcolor="#FFFFFA">
<form enctype="multipart/form-data" runat="server" id="form1">
<table runat="server" width=700 align=left id="table1" cellpadding=0 cellspacing =0 border=0>
<tr>
<td>上传图片</td>
<td>
<input type="file" id="up_file" runat="server" style="width:320" accept="text/*" name="up_file">
</td>
</tr>
<tr>
<td>文件说明</td>
<td>
<asp:TextBox runat="server" width=230 id="txtDesc" maintanstate="false" />
</td>
</tr>
<tr>
<td>
<asp:label runat="server" id="txtMess" forecolor=red maintainstate="false" />
</td>
<td>
<asp:Button runat="server" width=230 onclick="Button_Submit" text="上传" />
</td>
</tr>
</table>
</form>
</body>
</html> 显示的时候通过url后面的id来知道要读取数据库中哪条记录 
用Response.ContentType = (string)read["type"];输出到客户端时指定数据库中存好的word,图片是什么类型, 然后用OutputStream.Write输出, 
或用Response.BinaryWrite(byte[])来输出
输出后, 客户端IE可直接显示图片或word文档<%@Page language="C#"%>
<%@import namespace="System.Data"%>
<%@import namespace="System.Data.SqlClient"%>
<script language="C#" runat="server">
public void Page_Load(Object o, EventArgs e)
{
int ImgID = Convert.ToInt32(Request.Params["id"]);
string connStr = ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection conn = new SqlConnection(connStr);
string sql = "select * from t_imgs where id = @ImgID";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add("@ImgID", SqlDbType.Int).Value = ImgID;
conn.Open();
SqlDataReader read = cmd.ExecuteReader();
read.Read();
Response.ContentType = (string)read["type"];
Response.OutputStream.Write((byte[])read["imgData"], 0, (int)read["imgSize"]);
Response.End();
conn.Close();
}
</script>

解决方案 »

  1.   

    http://www.microsoft.com/china/msdn/library/dnexcl2k2/html/odc_offcs.asp
    http://expert.csdn.net/Expert/topic/1760/1760405.xml?temp=.1421167
      

  2.   

    /*          
    显示的时候通过url后面的id来知道要读取数据库中哪条记录 
    用Response.ContentType = (string)read["type"];输出到客户端时指定数据库中存好的word,图片是什么类型, 然后用OutputStream.Write输出, 
    或用Response.BinaryWrite(byte[])来输出
    输出后, 客户端IE可直接显示图片或word文档
    */

    string cFileNo = Request.Params["文件编号"];
    if(Request.Params["文件编号"]!=null)
    {
    string sql = "select * from tHrhRegTables where cFileNo = '"+ cFileNo +"'";
    if(oAccessData.sqlConn.State == System.Data.ConnectionState.Closed)
    {
    oAccessData.sqlConn.Open();
    }
    SqlCommand cmd = new SqlCommand(sql, oAccessData.sqlConn);
    // cmd.Parameters.Add("@FileNo", SqlDbType.VarChar,30).Value = cFileNo;

    SqlDataReader read = cmd.ExecuteReader();
    // read.Read();
    if(read.Read())
    {
    Response.ContentType = (string)read["cFileType"];
    byte[] btFileContent;
    btFileContent = new byte[((byte[])read["gElecDocu"]).Length];
    for(int i=0;i<btFileContent.Length;i++)
    {
    btFileContent[i] = ((byte[])read["gElecDocu"])[i];
    }
    Response.OutputStream.Write(btFileContent, 0, btFileContent.Length); Response.End();
    oAccessData.sqlConn.Close();
    }
    }