c# - 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"/>
解决方案
(移动评论回答)
尝试Coffee & Froth
或Coffee%20%26%20Froth
&
在 HTML 中用于表示&
%20
是十六进制space
并且%26
是十六进制&
。十六进制通常用于对 URL 中的特殊字符进行编码。
完整的 ASCII 表: https ://en.wikipedia.org/wiki/ASCII#Printable_characters
推荐阅读
- java - Android Studio/LibGDX:是否可以通过一次触摸按下两个按钮?
- java - 如何检查字符串是否只有数字
- php - php:向数组中的现有键添加值
- haskell - Haskell表达式用于改变其周围环境的函数?
- excel - 仅使用 VBA 填充包含数据的单元格的背景颜色?
- reactjs - axios 在帖子中不包括 firebase 标头
- amazon-web-services - 从 S3 获取最新文件失败并出现错误
- java - 有没有办法在 JScrollPane 上显示 2D 图形
- django - 如何自定义 django-allauth url
- javascript - 从角色获取所有成员仅返回 Bot 和发送命令的用户