开发者

HTTPHandler write Binary Data to Response , load image

iv'e got a repeater bonded to开发者_如何转开发 a list of entities

    BL_Engine engine = (BL_Engine)Application["engine"];
    List<AppProduct> products = engine.Get_Products();
    Repeater1.DataSource = products;
    Repeater1.DataBind();

iv'e also got a user control i use to represent these product entities , i do this by overriding the Databind() in the user control :

    public override void DataBind()
    {
        AppProduct product = (Page.GetDataItem() as AppProduct);
        lbl_title.Text = product.Title;
        lbl_short.Text = product.Short;
        lbl_date.Text = product.Date.ToShortDateString();                       
        Session["current_img"] = product.Image1;            
        base.DataBind();
    }

in my HttpHanlder object kept in a .ashx file i write the image to the response the response happens only once so only the last picture is written to (ALL) the user controls.

     public void ProcessRequest(HttpContext context)
     {
        byte [] image = (byte[])context.Session["current_img"];                  
        context.Response.ContentType = "image/bmp";
        context.Response.OutputStream.Write(image, 0, image.Length);
     }

any idea how i could write the binary data for each individual control

thanks in advance eran.


Let me suggest a different approach.

Declare a regular html image element in your control and set the "runat=server" property as so:

<img src="" runat="server" id="img_product" />

Then change your DataBind() method to do this instead:

 public override void DataBind()
 {
       AppProduct product = (Page.GetDataItem() as AppProduct);
       lbl_title.Text = product.Title;
       lbl_short.Text = product.Short;
       lbl_date.Text = product.Date.ToShortDateString();                       
       img_product.src= "\"data:image/jpg;base64,"+System.Convert.ToBase64String(product.Image1)+"\"";
        base.DataBind();
 }

And get rid of the HTTPHandler. You don't need it for this.


Change your handler so that it takes the id of the current product as a query string parameter. In the handler load the correct image data based on the parameter and write that instead.


well to conclude , for this case i think the best practice would be @icarus's answer , not the disregard @kmcc049's answer i just hadn't dug into it since it seemed like a more complicated architecture for my app.

in this case DROP THE HTTPHANDLER .

i saved the image's type , and data from the post file.

        public enum ImageType : byte {jpg,jpeg,png,gif}  

        private byte[] Get_Image(HttpPostedFile file)
        {
            ImageType type = GetType(file.ContentType); 
            if (file.InputStream.Length <= 1)
                return null;
            byte[] imageData = new byte[file.InputStream.Length + 1 + 1];
            file.InputStream.Read(imageData, 1, imageData.Length+1);
            imageData[0] =(byte)type;
            return imageData;
         }

         private ImageType GetType(string _type)
         {
            ImageType t = default(ImageType); 
            string s = _type.Substring(_type.IndexOf('/')+1).ToLower() ;
            switch (s)
            {
                 case "jpg": t = ImageType.jpg;
                      break;
                 case "jpeg": t = ImageType.jpeg;
                      break;
                 case "png": t = ImageType.png;
                      break;
                 case "gif": t = ImageType.gif;
                      break;
            }
            return t; 
       }

then i extracted and added it to the user control in my DataBind override(in my user control) :

       public override void DataBind()
       {
            AppProduct product = (Page.GetDataItem() as AppProduct);
            img_main.Attributes.Add("src",Build_Img_Data(product.Image1));                        
            base.DataBind();
       }


       private string Build_Img_Data(byte[] imageData)
       {
           ImageType type = (ImageType)imageData[0] ;
           byte[] new_imageData = new byte[imageData.Length - 1];
           Array.ConstrainedCopy(imageData, 1, new_imageData, 0, new_imageData.Length); 
           MemoryStream ms = new MemoryStream(new_imageData);
           string base64String = string.Format("data:image/{0};base64,{1}",type.ToString(),Convert.ToBase64String(ms.ToArray()));
           return base64String;
       }       
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜