HTTPHandler 将二进制数据写入 Response ,加载图像

发布于 2024-12-06 05:37:25 字数 1113 浏览 2 评论 0原文

iv'e 得到了一个绑定到实体列表的转发器

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

iv'e 还得到了一个用户控件,我用它来表示这些产品实体,我通过重写用户控件中的 Databind() 来实现这一点:

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

在我的 HttpHanlder 对象中,保存在 . ashx 文件我将图像写入响应 响应仅发生一次,因此只有最后一张图片被写入(全部)用户控件。

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

知道如何为每个单独的控件编写二进制数据,

提前致谢 埃兰。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南街九尾狐 2024-12-13 05:37:25

让我建议一种不同的方法。

在您的控件中声明一个常规 html 图像元素,并按如下方式设置“runat=server”属性:

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

然后更改您的 DataBind() 方法来执行此操作:

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

并摆脱 HTTPHandler。你不需要它。

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.

執念 2024-12-13 05:37:25

更改您的处理程序,使其将当前产品的 id 作为查询字符串参数。在处理程序中,根据参数加载正确的图像数据并将其写入。

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.

小情绪 2024-12-13 05:37:25

总而言之,对于这种情况,我认为最好的做法是@icarus的答案,
不是无视@kmcc049的答案,我只是没有深入研究它,因为它对于我的应用程序来说似乎是一个更复杂的架构。

在这种情况下,删除 HTTPHANDLER 。

我保存了图像的类型以及发布文件中的数据。

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

然后我提取并将其添加到我的 DataBind 覆盖中的用户控件(在我的用户控件中):

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

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;
       }       
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文