Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

[5223] Using ASHX files to retrieve DB images

Last post 05-09-2007 10:29 AM by fakhruddins128. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 01-01-1999 12:00 AM

    [5223] Using ASHX files to retrieve DB images

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

    • Post Points: 20
  • Advertisement

    • Red Gate Software

    Advertisement

    Want to boost your .NET application performance?

    Some developers always seem to write efficient and lightening-fast code. What is their secret? It’s ANTS Profiler. “We improved the performance of the application up to 10 times” Dan Ports, Intrigma.

    Try it for yourself now.

  • 04-22-2006 7:00 PM In reply to

    • semma
    • Not Ranked
    • Joined on 04-22-2006
    • United States
    • New Member
    • Points 5

    Re: [5223] Using ASHX files to retrieve DB images - Modified - Download PDF stored in SQL Server Database

    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);

    }

    }

     

    • Post Points: 5
  • 11-13-2006 4:33 PM In reply to

    • kidsonk
    • Not Ranked
    • Joined on 11-13-2006
    • New Member
    • Points 5

    Re: [5223] Using ASHX files to retrieve DB images

    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 ??

    • Post Points: 5
  • 05-09-2007 10:29 AM In reply to

    Re: [5223] Using ASHX files to retrieve DB images

    Hi

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

     

     

    • Post Points: 5
Page 1 of 1 (4 items)