【问题标题】:how do I retrieve multiple images from the database using c#如何使用 c# 从数据库中检索多个图像
【发布时间】:2011-08-26 08:48:22
【问题描述】:

我有一个包含 9 张图片的数据库,这些图片一直在变化,所以我不能直接在 html <img> 标签中设置 src 来显示这 9 张图片,我必须从数据库中挑选它们并相应地绑定它们。

我可以使用 Response.BinaryWrite() 检索和打印 1 张图像,但不是全部 9 张。我的 byte[] 仅从数据库中获取第一张图像,

这是代码,

            while (dr.Read())
            {
                Response.ContentType = "image/jpg";
                Response.BinaryWrite((byte[])(dr["fsImage"]));

            }

如何检索所有 9 张图片,以及如何将它们绑定到 <asp:Image> 标签或动态构造一个 html <img> 标签

我是新手,如果有愚蠢的错误,请不要客气;)

提前致谢。

【问题讨论】:

  • 如果您只是将 URL 存储在数据库中,然后设置 href 标签,您的生活会更轻松。你有什么理由不能这样做吗?请参阅下面的大纲,了解如何以艰难的方式做到这一点。
  • @Hogan 该死的,从来没有想过,会试试看!任何解决我的问题的链接都会有很大帮助!

标签: c# asp.net database image sqldatareader


【解决方案1】:

你不能让网页给出所有图片的内容。

为了“动态”渲染图像,您需要创建一个页面,该页面将为您提供单个图像。此页面将使用您上面的代码。

然后选择哪个图像来显示页面的 url 会改变例如

http://youdomain/makeimage.aspx?imageid=1

http://youdomain/makeimage.aspx?imageid=2

make image 中的代码将返回单个图像。

注意——如果你把网址写出来,你会更开心

http://youdomain/makeimage.aspx?imageid=1&mytype=.jpg

因此,图像 url 以扩展名结尾。一些浏览器 (IE) 作弊并查看字符串的末尾以查看预期的内容类型。他们将使用像上面这样的 url,但不能没有试用 .jpg。

此外,您将需要实现某种形式的缓存,我们的表现将是一条狗(尤其是如果您尝试扩展。)

祝你好运。

【讨论】:

  • 我也会采用的方法。不过需要注意的是,指定 mime 类型会让所有浏览器都满意,并且不需要在末尾添加“.jpg”
  • @paan - 不是我的经验 - 有些浏览器仍然存在问题。是的,您还必须指定 mime 类型。在所有示例中都是如此 - 我正在对一个难以找到的问题进行说明。
  • 注明。也许我从来没有遇到过任何问题。你说的对。一个简单的额外步骤可确保在网络上始终保持良好的兼容性
【解决方案2】:

使用HttpHandler并调用Img标签

一个例子

<%@ WebHandler Language="C#" Class="Handler2" %>

using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;


public class Handler2 : IHttpHandler {


    public void ProcessRequest(HttpContext context)
    {

        if (context.Request.QueryString["pid"] != null)
        {
            string ID = context.Request.QueryString["pid"].ToString();

            if (dt.Rows.Count > 0)
            {
                int pid = Convert.ToInt32(ID);
                SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);
                myConnection.Open();
                //int i = Convert.ToInt32(context.Request.QueryString["id"]);
                string sql = "Select BackGroundImage from Wall_BackgrndImg_Master where FK_Company_Master=@ImageId";
                SqlCommand cmd = new SqlCommand(sql, myConnection);
                cmd.Parameters.Add("@ImageId", SqlDbType.Int).Value = pid;
                cmd.Prepare();
                SqlDataReader dr = cmd.ExecuteReader();
                dr.Read();
                try
                {
                    context.Response.ContentType = "jpg";
                    context.Response.BinaryWrite((byte[])dr["BackGroundImage"]);
                    dr.Close();
                    myConnection.Close();
                }
                catch
                {

                }
            }
        }

    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

然后像打电话一样

    <img src="~/handlor.ashx?pid=1">
<img src="~/handlor.ashx?pid=2">
<img src="~/handlor.ashx?pid=3">
<img src="~/handlor.ashx?pid=4">
<img src="~/handlor.ashx?pid=5">

【讨论】:

  • 这是一个比 aspx 页面更好的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-30
  • 1970-01-01
  • 2021-10-06
相关资源
最近更新 更多