Ajax slide show get images from database

I had sidehow ajax and I want it to get the image database I used sql server 2000 and I had binary images

this is my code for selecting images from the database

public class SlidShow : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        using (SqlConnection con = Connection.GetConnection())
        {
            string Sql = "Select image from SlideShowImage Where  Active=1 And Hig_Id=@Hig_Id";
            System.Data.SqlClient.SqlCommand com = new SqlCommand(Sql, con);
            com.CommandType= System.Data.CommandType.Text;
            com.Parameters.Add(Parameter.NewInt("@Hig_Id", context.Request.QueryString["Hig_ID"].ToString()));

            System.Data.SqlClient.SqlDataReader dr = com.ExecuteReader();
            if (dr.Read() && dr != null)
            {

                Byte[] bytes1 = (Byte[])dr["image"];

                context.Response.BinaryWrite(bytes1);

                dr.Close();
            }
        }
    }

  [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod]

    public static AjaxControlToolkit.Slide[] GetSlides()
    {


        return new AjaxControlToolkit.Slide[] { 
            new AjaxControlToolkit.Slide("images/sharp_highlight_ref_img.jpg", "", ""),
             new AjaxControlToolkit.Slide("images/products_fridg_img.jpg", "", ""),
            new AjaxControlToolkit.Slide("images/sharp_home_highlight_img.jpg", "", "")


        };

    }
}
0
source share
1 answer

I would download the image as an HttpHandler , which should be registered in web.config. You do not need Ajax to download the image. Your javascript code must change the attribute of srcyour tag imgto display a new image.

Here is an example http handler that downloads a blog from MS SQL db with a query parameter id.

public class IISHandler1 : IHttpHandler
{
    public bool IsReusable
    {
        get { return true; }
    }

    public void ProcessRequest(HttpContext context)
    {
        int theID;
        if (!int.TryParse(context.Request.QueryString["id"], out theID))
            throw new ArgumentException("No parameter specified");

        context.Response.ContentType = "image/jpeg"; // or gif/png depending on what type of image you have
        Stream strm = DisplayImage(theID);
        byte[] buffer = new byte[2048];
        int byteSeq = strm.Read(buffer, 0, 2048);
        while (byteSeq > 0)
        {
            context.Response.OutputStream.Write(buffer, 0, byteSeq);
            byteSeq = strm.Read(buffer, 0, 2048);
        }
    }

    public Stream DisplayImage(int theID)
    {
        try
        {
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString());
            string sql = "SELECT image FROM Table1 WHERE id = @ID";
            using (SqlCommand cmd = new SqlCommand(sql, connection) { CommandType = CommandType.Text })
            {
                cmd.Parameters.AddWithValue("@ID", theID);
                connection.Open();
                object theImg = cmd.ExecuteScalar();
                return new MemoryStream((byte[]) theImg);
            }
        }
        catch
        {
            return null;
        }
    }
}
+1
source

Source: https://habr.com/ru/post/1738393/


All Articles