首页 > 解决方案 > Display.ashx 不显示带有字符 & 的图像名称

问题描述

我有一种情况,Display.ashx 成功显示来自 Sql 服务器的图像,其中只有字母(例如咖啡或绿茶),但是当图像名称保存为(咖啡和泡沫)时,它不显示。请参阅下面的代码,我在 Display 类中的 Img 上放置了一个断点,当图像名称为 Coffee 时,传递了完整的字符串,但当图像名称为 Coffee & Froth 时,它会减少泡沫并且只需要 Coffee,这意味着它只需要字符 &.can 之前的字符串有人请告诉我一个解决方法,以便将整个字符串与字符一起使用。谢谢

 public class Display : IHttpHandler
    {
        public Stream Displays(string Img)
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DB"].ConnectionString.ToString()))
            {
                string sql = string.Format("select top 1 img from Images where Name  = '{0}'", Img);

                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.CommandType = CommandType.Text;
                cmd.Connection.Open();

                object imagevalue = cmd.ExecuteScalar();

                if (imagevalue != null && imagevalue != DBNull.Value)
                {
                    return new MemoryStream((byte[])imagevalue);
                }

                using (FileStream fs = File.OpenRead(HttpContext.Current.Server.MapPath(@"~/Content/noimage.png")))
                {
                    MemoryStream memStream = new MemoryStream();
                    memStream.SetLength(fs.Length);
                    fs.Read(memStream.GetBuffer(), 0, (int)fs.Length);
                    return memStream;
                }
            }
        }
    #endregion

这就是我所说的图像

 protected void Refresh_SelectedIndexChanged(object sender, EventArgs e)
        {
            // string id = CoffeeMenu.SelectedValue;
            Image1.ImageUrl = "~/Display.ashx?id=" + "Coffee & Froth";
            divSuccess.Visible = false;
          divError.Visible = false;
        }

HTML

  <asp:DropDownList ID="CoffeeMenu" runat="server" CssClass="selectpicker" OnSelectedIndexChanged="Refresh_SelectedIndexChanged"/>

标签: c#htmlasp.netwebforms

解决方案


(移动评论回答)

尝试Coffee &amp; FrothCoffee%20%26%20Froth

&amp;在 HTML 中用于表示&

%20是十六进制space并且%26是十六进制&。十六进制通常用于对 URL 中的特殊字符进行编码。

完整的 ASCII 表: https ://en.wikipedia.org/wiki/ASCII#Printable_characters


推荐阅读