加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

用Sqlserver中的text类型存储图片

发布时间:2020-12-12 14:48:33 所属栏目:MsSql教程 来源:网络整理
导读: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" tit
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; } } }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读