微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

用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,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,pict.Length);                     }                 }             }         }

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐