Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

c# - How to get particular image from the database?

I have an image control on the aspx page like this

    <asp:Image ID="Image1" runat="server" Height="64px" Width="64px" 
     ImageUrl='<%# "SideImageHandler.ashx?ID=" + Eval("ID")%>'/>

And my imagehandler code looks like this

    public void ProcessRequest(HttpContext context)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["GalleryConnectionString"].ConnectionString;

        // Create SQL Command 
        Utility.ImageID = 2;
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "SELECT  IMAGE FROM  Icon WHERE (ID ="+ Utility.ImageID+")";
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.Connection = con;

        SqlParameter ImageID = new SqlParameter("@ID", System.Data.SqlDbType.Int);
        ImageID.Value = context.Request.QueryString["ID"];
        cmd.Parameters.Add(ImageID);
        con.Open();
        SqlDataReader dReader = cmd.ExecuteReader();
        dReader.Read();
        context.Response.BinaryWrite((byte[])dReader["IMAGE"]);
        dReader.Close();
        con.Close();
    }

But it is not showing me the image. whats going wrong with it?

Also, I have a download button when the user clicks on it the image will be downloaded I'm newer don't know what the code I put on the download button click event? Kindly guide me thanks in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is just sample.Use:

  <asp:image id="Image1" imageUrl="SideImageHandler.ashx?ID=<someId>"/>

add this in config:

<httpHandlers>
  <add verb="*" path="img/*" type="SideImageHandler"/>
</httpHandlers>

and in handler:

  public void ProcessRequest (HttpContext context) 
    { 
          int ID;
          if (context.Request.QueryString["ID"] != null)
               ID= Convert.ToInt32(context.Request.QueryString["ID"]);
          else
            throw new ArgumentException("No parameter specified");

        byte[] imageData= ;//get the image data from the database using the employeeId Querystring
        Response.ContentType = "image/jpeg";
        Response.BinaryWrite(imageData);

    } 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...