用Sqlserver中的text类型存储图片

create table testTB
(
   ID int,
   [file] text
)

 

Upload.aspx 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:FileUpload ID="file" runat="server" />
    <asp:Button ID="btn" runat="server" Text="提交" OnClick="btn_Click" />
    <asp:GridView ID="gv" runat="server">
        <Columns>
            <asp:ImageField DataImageUrlField="id" DataImageUrlFormatString="Show.ashx?id={0}">
            </asp:ImageField>
            <asp:HyperLinkField HeaderText="连接地址" Text="查看" DataNavigateUrlFields="id" DataNavigateUrlFormatString="Show.aspx?id={0}"
                Target="_blank" />
        </Columns>
    </asp:GridView>
    <asp:Repeater ID="rp" runat="server">
        <HeaderTemplate>
            <table>
                <tr>
                    <td>
                        ID
                    </td>
                    <td>
                        图片
                    </td>
                </tr>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td>
                    <%#Eval("ID") %>
                </td>
                <td>
                    <img src='<%#"Show.ashx?id="+Eval("ID") %>' />
                </td>
            </tr>
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:Repeater>
    </form>
</body>
</html>


Upload.aspx.cs

public partial class UploadFile : System.Web.UI.Page
    {
        public const string ConnStr = "Data Source=192.168.0.74;Initial Catalog=test;uid=xxx;pwd=xxx";

        private void Bind()
        {
            SqlDataAdapter da = new SqlDataAdapter("select id from testTB", ConnStr);
            DataTable dt = new DataTable();
            da.Fill(dt);
            gv.DataSource = dt;
            gv.DataBind();
            rp.DataSource = dt;
            rp.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }

        protected void btn_Click(object sender, EventArgs e)
        {
            if (file.PostedFile != null && file.PostedFile.ContentLength > 0)
            {
                int size;
                Stream fileStream;

                size = file.PostedFile.ContentLength;
                fileStream = file.PostedFile.InputStream;
                Byte[] fileContent = new Byte[size];
                fileStream.Read(fileContent, 0, size);

                using (SqlConnection con = new SqlConnection(ConnStr))
                {
                    using (SqlCommand cmd = new SqlCommand("insert into testTB values(@ID,@File)", con))
                    {
                        cmd.CommandType = CommandType.Text;

                        SqlParameter id = new SqlParameter("@ID", SqlDbType.Int);

                        id.Value = DateTime.Now.Millisecond;
                        cmd.Parameters.Add(id);

                        SqlParameter filePara = new SqlParameter("@File", SqlDbType.Text);

                        filePara.Value = Convert.ToBase64String(fileContent);

                        cmd.Parameters.Add(filePara);

                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                        Bind();
                        Response.Write("success");
                    }
                }
            }
        }
    }


Show.ashx.cs

    public class Show : IHttpHandler
    {      

public void ProcessRequest(HttpContext context)         {

            string id = context.Request.QueryString["id"];

            if (!string.IsNullOrEmpty(id))             {                 using (SqlConnection con = new SqlConnection(UploadFile.ConnStr))                 {                     using (SqlCommand cmd = new SqlCommand("select [file] from testTB where id = @ID", con))                     {                         cmd.CommandType = CommandType.Text;                         SqlParameter idPara = new SqlParameter("@ID", SqlDbType.Int);                         idPara.Value = id;                         cmd.Parameters.Add(idPara);                         con.Open();                         byte[] pict = Convert.FromBase64String((string)cmd.ExecuteScalar());                         con.Close();                         context.Response.ContentType = "image/bmp";                         context.Response.OutputStream.Write(pict, 0, pict.Length);                     }                 }             }         }

 

public bool IsReusable { get { return false; } } }


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值