mvc 显示数据库图片

来源:互联网 发布:数据驱动安全 编辑:程序博客网 时间:2024/05/12 10:20

 <%
        var ksList = ViewData["KSTD"] as IList<Model.DB_LINKS>;
        if (ksList != null)
        {
            for (int i = 0; i < ksList.Count; i++)
            {
    %>
   
        <div style="height: 50px; background: url(<%= Url.Content("~/DisplayImg.aspx?linkId="+ksList[i].LINKSID) %>) no-repeat; text-align:center; vertical-align:middle;">
          <a href="<%= Url.Content(ksList[i].URL) %>" target="_blank" style="margin:0 0 0 0; color:#ffffff; font-size:24px;">  <%= ksList[i].LINKSTITLE %></a></div>
   
    <%                            
            }
        }
    %>

 

<%@ Page Language="C#" AutoEventWireup="true" %>

<script runat="server">

    protected override void OnLoad(EventArgs e)
    {
        if (Request.QueryString["linkId"] != null)
        {
            BLL.DB_LINKS bll = new BLL.DB_LINKS();
            Model.DB_LINKS m = bll.GetModel(Request.QueryString["linkId"].ToString());
            if (m == null) return;
           
            byte[] buffer = m.IMG;
            if (buffer != null)
            {
                using (System.IO.MemoryStream stream = new System.IO.MemoryStream(buffer))
                {
                    System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
                    HttpContext.Current.Response.Clear();
                    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);

                    HttpContext.Current.Response.ContentType = "image/jpg";
                    if (image != null)
                    {
                        image.Save(HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                    }
                }

            }
        }       
    }

</script>

 

原创粉丝点击