上传图片到数据库及显示

上传
htrk.aspx
<%@ Register TagPrefix="uc1" TagName="Menu1" Src="Menu1.ascx" %>
<%@ Register TagPrefix="uc1" TagName="Menu" Src="Menu.ascx" %>
<%@ Page language="c#" Codebehind="htrk.aspx.cs" AutoEventWireup="false" Inherits="project.upload2" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>upload2</title>
  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
  <meta content="C#" name="CODE_LANGUAGE">
  <meta content="JavaScript" name="vs_defaultClientScript">
  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="uploderform" action="upload.aspx" method="post" encType="multipart/form-data"
   runat="server">
   <FONT face="宋体"></FONT><FONT face="宋体"></FONT><FONT face="宋体"></FONT><FONT face="宋体">
   </FONT><FONT face="宋体"></FONT><FONT face="宋体"></FONT><FONT face="宋体"></FONT><FONT face="宋体">
   </FONT><FONT face="宋体"></FONT>
   <uc1:Menu1 id="Menu11" runat="server"></uc1:Menu1><br>
   <br>
   <br>
   <table style="Z-INDEX: 102; LEFT: 312px; WIDTH: 352px; POSITION: absolute; TOP: 32px; HEIGHT: 160px"
    borderColor="#000000" cellSpacing="0" width="352" border="1">
    <tr>
     <td style="HEIGHT: 26px" align="center" colSpan="2">&nbsp;合同录入</td>
    </tr>
    <tr>
     <td style="WIDTH: 107px; HEIGHT: 1px" width="107">&nbsp;<FONT face="宋体"><FONT face="宋体">合同名称:</FONT></FONT></td>
     <td style="HEIGHT: 1px" width="91">&nbsp;
      <asp:textbox id="htmc" runat="server" Width="240px" BorderWidth="0px"></asp:textbox></td>
    </tr>
    <tr>
     <td style="WIDTH: 107px; HEIGHT: 27px">&nbsp;<FONT face="宋体"><FONT face="宋体">合同代码:</FONT></FONT></td>
     <td style="HEIGHT: 27px">&nbsp;
      <asp:textbox id="htdm" runat="server" Width="240px" BorderWidth="0px"></asp:textbox></td>
    </tr>
    <tr>
     <td style="WIDTH: 107px; HEIGHT: 25px">&nbsp;<FONT face="宋体">页&nbsp;&nbsp;&nbsp; 号: </FONT>
     </td>
     <td style="HEIGHT: 25px">&nbsp;
      <asp:textbox id="yh" runat="server" Width="240px" BorderWidth="0px"></asp:textbox></td>
    </tr>
    <tr>
     <td style="WIDTH: 107px; HEIGHT: 25px"><FONT face="宋体">选择合同: </FONT>
     </td>
     <td>&nbsp;<INPUT id="myFile" style="WIDTH: 240px; HEIGHT: 22px" type="file" size="20" name="myFile"
       runat="server"></td>
    </tr>
    <TR>
     <TD align="center" colSpan="2"><FONT face="宋体"><input id="Button1" style="WIDTH: 69px; HEIGHT: 24px" type="button" value="Upload" name="Button1"
        runat="server"></FONT></TD>
    </TR>
   </table>
  </form>
  <asp:label id="Label1" style="Z-INDEX: 103; LEFT: 312px; POSITION: absolute; TOP: 216px" runat="server"></asp:label>
 </body>
</HTML>

htrk.aspx.cs

using System;
using System.IO;
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.Configuration;
using System.Data.SqlClient;
namespace project
{
 /// <summary>
 /// upload2 的摘要说明。
 /// </summary>
 public class upload2 : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Label fname;
  protected System.Web.UI.WebControls.Label fenc;
  protected System.Web.UI.WebControls.Label fsize;
  protected System.Web.UI.WebControls.Label Label1;
  protected System.Web.UI.HtmlControls.HtmlInputButton Button1;
  protected System.Web.UI.HtmlControls.HtmlInputFile myFile;
  protected System.Web.UI.WebControls.TextBox htdm;
  protected System.Web.UI.WebControls.TextBox yh;
  protected System.Web.UI.WebControls.Label yh1;
  protected System.Web.UI.WebControls.TextBox htmc;
  protected System.Web.UI.WebControls.Label htmc1;
     public string htdz;

  private void Page_Load(object sender, System.EventArgs e)
  {  
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Button1.ServerClick += new System.EventHandler(this.Button1_ServerClick);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void Button1_ServerClick(object sender, System.EventArgs e)
  {
   if(myFile.PostedFile!=null)
   {    
    string nam = myFile.PostedFile.FileName ;
    int i= nam.LastIndexOf("//");
    
                string newnm =nam.Substring(i) ;
    FileInfo fi1 = new FileInfo(nam);

    project.users uploadht=new project.users();
    UpLoadHt(fi1,htdm.Text,htmc.Text,yh.Text);
                Label1.Text = "合同上传成功!";

    //取得上传文件的各种属性。
    fname.Text=myFile.PostedFile.FileName;
    fenc.Text=htdm.Text;
    fsize.Text=myFile.PostedFile.ContentLength.ToString();
    yh1.Text = yh.Text;
    htmc1.Text = htmc.Text;
   }
  }
  
  /// <summary>
  /// 向数据库写入图像文件
  /// </summary>
  public void UpLoadHt(FileInfo obj,string htdm,string htmc,string yh)
  {
    //创建一字节数组,用来存储图像文件.(数组的长度是图像文件的长度)
    byte[] Content=new byte[obj.Length];
    //打开文件并用他初始化一个文件流对象
    FileStream ImageFileStream=obj.OpenRead();
    //将文件内容写入字节数组
    ImageFileStream.Read(Content,0,Content.Length);
    //关闭文件流
    ImageFileStream.Close();

    //创建一个Sql命令对象,用来插入记录
                SqlConnection MySqlCon = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
    SqlCommand Command=new SqlCommand("INSERT Into htxx(htdm,htmc,yh,content) Values(@htdm,@htmc,@yh,@content)",MySqlCon);

    //图像的描述信息
    SqlParameter htmcParameter=new SqlParameter("@htmc",SqlDbType.NVarChar);
    htmcParameter.Value=htmc;
    Command.Parameters.Add(htmcParameter);

    //图像的数据字节数组
    SqlParameter contentParameter=new SqlParameter("@content",SqlDbType.Image);
    contentParameter.Value=Content;
    Command.Parameters.Add(contentParameter);

    //图像的大小信息
    SqlParameter htdmParameter=new SqlParameter("@htdm",SqlDbType.Int);
    htdmParameter.Value=htdm;
    Command.Parameters.Add(htdmParameter);

    //图像的类型新信息
    SqlParameter yhParameter=new SqlParameter("@yh",SqlDbType.NVarChar);
    yhParameter.Value=yh;
    Command.Parameters.Add(yhParameter);
   
    //打开数据库连接
    MySqlCon.Open();
    //执行 Sql 语句
    Command.ExecuteNonQuery();
    //关闭数据库连接
    MySqlCon.Close();

  }
 }
  
 }
}

显示
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;
using System.Data.OleDb;
using System.Configuration;
namespace project
{
 public class htll2 : System.Web.UI.Page
 { 
  private void Page_Load(object sender, System.EventArgs e)
  {
   if(Session["id"]!= null)
   {
    SqlConnection MySqlCon = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
    SqlCommand Command=new SqlCommand("select * from htxx where id = '"+Session["id"]+"'",MySqlCon);
   
    MySqlCon.Open();
    SqlDataReader MyReader=Command.ExecuteReader(CommandBehavior.CloseConnection);

    MyReader.Read();
    Response.BinaryWrite((byte[])MyReader["content"]);    
    Response.End();
    MyReader.Close();
   }
  }
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值