Community discussion forum

Using ASHX files to retrieve DB images

This is a comment thread discussing Using ASHX files to retrieve DB images
  • 9 years ago

    This thread is for discussions of Using ASHX files to retrieve DB images.

  • Advertisement

    Simply the fastest line-level profiler for .NET ever

    “The low overhead means it has minimal impact on the execution of my program”
    Mark Everest, Development Team Leader, Renault F1 Team Ltd.

    Try out the new ANTS Profiler 4 for yourself. Download your 14-day trial now

  • 2 years ago

    Thank you, thank you, thank you!  

    For those gurus out there, I hope I am not gushing too much.  I have been working for a week trying to find out how to get a pdf out of sql server in asp.net 2.0.  I made one small code tweak to the above and it worked flawlessly!!!  Thanks Again.... I'll buy the beer next time you are in Massachusetts!!!! 

    Here's my tweak:  I just call the page with http://mysite.com/handler.ashx?id=xxx  and viola!

    <%

    @ webhandler language="C#" class="NWEmpPhotoHandler" %>

    using

    System;

    using

    System.Web;

    using

    System.Data;

    using

    System.Data.SqlClient;

    public

    class NWEmpPhotoHandler : IHttpHandler

    {

    public bool IsReusable { get { return true; } }

    public void ProcessRequest(HttpContext ctx)

    {

    string id = ctx.Request.QueryString["id"];

    SqlConnection con = new SqlConnection("Data Source=server;Initial Catalog=schema;Persist Security Info=True;User ID=userid;Password=password");

    SqlCommand cmd = new SqlCommand("SELECT Image FROM mytable WHERE DocID = @DocID", con);

    cmd.CommandType =

    CommandType.Text;

    cmd.Parameters.Add(

    "@DocID", id);

    con.Open();

    byte[] pict = (byte[])cmd.ExecuteScalar();

    con.Close();

    ctx.Response.ContentType =

    "application/pdf";

    // ctx.Response.OutputStream.Write(pict, 78, pict.Length - 78);

    ctx.Response.BinaryWrite(pict);

    }

    }

     

  • 2 years ago

    Brilliant piece of code - and is far faster than a standard aspx page

    If a suggestion is in order - a trap to see if a value is returned and if not, to show a blank image ( thus not showing the broken image link

    if (pict.Length > 0){
            ctx.Response.ContentType = "image/jpeg";
            ctx.Response.OutputStream.Write(pict,0,pict.Length);
          }
     else {
       Image pict2 = Image.FromFile("C:\\somedirectory\\blankimage.gif");
      ctx.Response.ContentType="image/jpeg";
      pict2.Save(ctx.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);






    }

    This however bombs out on pict.Length if no id is supplied - any suggestions ??

  • 1 year ago

    Hi

    I read your post its realy nice. kindly explan how to pass ID with <img> or <asp:image>

     

     

Post a reply

Enter your message below

Sign in or Join us (it's free).