显示数据库存储图像的搜索结果,无文件扩展名

时间:2017-01-13 09:56:46

标签: javascript jquery asp.net

我有一个自动填充搜索框,可以剔除存储在数据库中的图像缩略图。问题是,每次图像名称出现在自动填充搜索框中时,.jpg扩展名都会显示。无论如何我可以修改这些代码以防止.jpg扩展出现吗?下面是我使用javascript和ashx的现有代码。 提前谢谢!

aspx:

<script src="scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="scripts/jquery.autocomplete.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#<%=txtSearch.ClientID%>").autocomplete("Search.ashx", {
                width: 200,
                formatItem: function (data, i, n, value) {
                    return "<img style = 'width:50px;height:50px' src= 'Images/" + value.split(",")[1] + "'/> " + value.split(",")[0];
                },
                formatResult: function (data, value) {
                    return value.split(",")[0];
                }
            });
        });
</script>

<style type="text/css">
    .Gridview {
        font-family: Verdana;
        font-size: 10pt;
        font-weight: normal;
        color: black;
    }
</style>
</head>
    <body>
        <form id="form1" runat="server">
            <div>
                Search here: 
        <asp:TextBox ID="txtSearch" runat="server" />
                <br />
            </div>

            <div>
                <asp:FileUpload ID="fileuploadimages" runat="server" />
                <br />
                <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
            </div>
            <div>
            </div>
            <asp:Label ID="lblResult" runat="server"></asp:Label>
        </form>
    </body>
</html>

ashx:

public class Search : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        string searchText = context.Request.QueryString["q"];
        string strcon = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
        SqlConnection connection = new SqlConnection(strcon);
        connection.Open();
        SqlCommand cmd = new SqlCommand("select ID,ImageName,ImagePath from ImagesPath where ImageName Like @Search + '%'", connection);
        cmd.Parameters.AddWithValue("@Search", searchText);
        StringBuilder sb = new StringBuilder();
        using (SqlDataReader dr = cmd.ExecuteReader())
        {
            while (dr.Read())
            {
                sb.Append(string.Format("{0},{1}{2}", dr["ImageName"], dr["ImageName"], Environment.NewLine));
            }
        }
        connection.Close();
        context.Response.Write(sb.ToString());
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

在ashx中进行搜索和替换

dr["ImageName"].ToString().Replace(".jpg", "");

或者在数据库中

select ID, REPLACE(ImageName, '.jpg', '') AS ImageName, ImagePath from ImagesPath

或者,如果您有多个扩展程序,也可以执行此操作

Path.GetFileNameWithoutExtension(dr["ImageName"].ToString())

这会筛选出每个文件的扩展名,以及它应该存在的路径:

string file = @"c:\folder\testFile.jpg";
string result = Path.GetFileNameWithoutExtension(file);

//result: testFile

转换为您的代码段,它看起来像这样。

while (dr.Read())
{
    string file = Path.GetFileNameWithoutExtension(dr["ImageName"].ToString());
    sb.Append(string.Format("{0},{1}{2}", file, file, Environment.NewLine));
}