Reloading Binary Image

8 posts, 0 answers
  1. Francisco
    Francisco avatar
    15 posts
    Member since:
    Sep 2010

    Posted 07 Mar 2011 Link to this post

    Hello,

    I'm using a RadBinaryImage to show an image stored in a folder. Using a RadAsyncUpload, the users are able to upload a new image and the RadBinaryImage will work as a thumbnail. This is working correctly, the thumbnail is reloaded correctly and the image is phisically stored, but once I go to another page and try to return to the page where the image is stored it always shows the image that was showing before.

    I think that this is happening because the RadBinaryImage is using the Cache, which is very usefull since the image won't be changed very often, but I need to disabled the cache when a new image is loaded or try to reload the image.

    Here is a bit of Code:

    <telerik:RadBinaryImage runat="server" Width="740px" Height="600px" ResizeMode="Fit"
        ID="RadImageOrganizationChart" AlternateText="OrganizationChart" ImageUrl="Images/Chart.jpg"/>

    /// <summary>
    /// Loads the image before uploading it
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void AsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e)
    {
        RadImageOrganizationChart.Width = Unit.Pixel(800);
        RadImageOrganizationChart.Height = Unit.Pixel(600);
     
        using (Stream stream = e.File.InputStream)
        {
            byte[] imageData = new byte[stream.Length];
            stream.Read(imageData, 0, (int)stream.Length);
            RadImageOrganizationChart.DataValue = imageData;
        }
     
            if (AsyncUpload1.UploadedFiles.Count > 0)
            {
                UploadedFile file = AsyncUpload1.UploadedFiles[0];
                string fullPath = Server.MapPath("~/Images");
                string fileName = "Chart.jpg";
                file.SaveAs(Path.Combine(fullPath, fileName)); 
            }
     
    }
  2. Tsvetina
    Admin
    Tsvetina avatar
    1876 posts

    Posted 10 Mar 2011 Link to this post

    Hi Francisco,

    The RadBinaryImage control uses an internal http handler which streams the image from the binary source to the page in which it has to be visualized.The storage of the binary stream when transferred between the control itself and the handler is the HttpContext.Current.Cache object and the image is cached in the browser. Its default expiration time is 2 hours. This means that subsequent loads of the binary image are taken from the browser cache when using the same url for access.

    In order to reload the image and not take it from the cache, you can rebind or recreate the control in which the RadBinaryImage is nested.

    Regards,
    Tsvetina
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Francisco
    Francisco avatar
    15 posts
    Member since:
    Sep 2010

    Posted 10 Mar 2011 Link to this post

    Hello Tsevetina,

    Thanks for your answer, this is something I found on the documentation, but I didn't understand what does "rebind or recreate the control in which the RadBinaryImage is nested" means. If my RadBinaryImage is merely in a form, does it mean I need to rebind the whole form?

    Kind Regards,
  5. Tsvetina
    Admin
    Tsvetina avatar
    1876 posts

    Posted 15 Mar 2011 Link to this post

    Hi Francisco,

    By default each time the DataValue of RadBinaryImage is changed, it caches the new value and generates a new unique url pointing to it. This means that the described behavior should not be happening.

    I would recommend you to first try removing this setting form your RadBinaryImage declaration:
    ImageUrl="Images/Chart.jpg"
    because it might be preventing the control from generating unique urls.

    If this does not help, please provide us with more details on how the issue is replicated (in what browser, are you returning by revisiting the link or using the back button, etc.) and we will investigate it further.

    Greetings,
    Tsvetina
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  6. Francisco
    Francisco avatar
    15 posts
    Member since:
    Sep 2010

    Posted 15 Mar 2011 Link to this post

    Hi Tsvetina,

    I already tried to reset the ImageUrl, with no sucess.

    I'm using a button to redirect to my previous page (Response.Redirect(...)), but if I do right click and reload the page the image reloads correctly.

    This is a strange behavior, am I doing something wrong?

    protected void RadButtonChart_Click(object sender, EventArgs e)
    {
        Response.Redirect("Chart.aspx");
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (RadImage != null)
            {
                RadImageChart.DataValue = null;
                RadImageChart.DataBind();
                RadImageChart.Visible = true;
                RadImageChart.ImageUrl = "Images/Chart.jpg";
     
            }
        }
    }
            protected void AsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e)
            {
                RadImageChart.Width = Unit.Pixel(800);
                RadImageChart.Height = Unit.Pixel(600);
     
                using (Stream stream = e.File.InputStream)
                {
                    byte[] imageData = new byte[stream.Length];
                    stream.Read(imageData, 0, (int)stream.Length);
                     RadImageChart.DataValue = imageData;
                }
                try
                {
                    if (AsyncUpload1.UploadedFiles.Count > 0)
                    {
                        UploadedFile file = AsyncUpload1.UploadedFiles[0];
                        string fullPath = Server.MapPath("~/Images");
                        string fileName = "Chart.jpg";
                        file.SaveAs(Path.Combine(fullPath, fileName));
                    }
                }
    }


    protected void RadButtonReturn_Click(object sender, EventArgs e)
    {
        RadImageChart.Visible = false;
        Response.Redirect("PreviousPage.aspx");           
    }
  7. Tsvetina
    Admin
    Tsvetina avatar
    1876 posts

    Posted 18 Mar 2011 Link to this post

    Hello Francisco,

    What Response.Redirect() does is an initial load of the target page (as if you have never loaded it before). Therefore, it is expected that the binary image control does not keep the last uploaded image. This also explains why the initial image is shown - it is assigned in the Page_Load event handler on initial load, which is hit when you are redirected back to the page.

    There would be a problem with the binary image control in case you were going back to the initial page using the browser Back button (because it does not redirect, it just loads the page from cache).

    Kind regards,
    Tsvetina
    the Telerik team
  8. BESI
    BESI avatar
    1 posts
    Member since:
    Dec 2010

    Posted 29 Mar 2011 Link to this post

    I don't understand, because I'm saving the image to a path which is used to load the image in the Page_Load(), the image is saving succesfuly but I thought that in Page_Load the RadBinaryImage will read again the url and load the image.

    If this is not possible, how I can achieve to load an image in a RadBinaryImage and save it to a file, so it will be loaded again for the rest of the users?

    Kind Regards,
  9. Tsvetina
    Admin
    Tsvetina avatar
    1876 posts

    Posted 01 Apr 2011 Link to this post

    Hello Fernando,

    I am not sure if this is the same problem as the previously described. I would advise you to open a formal support ticket and send us a runable version of your code and let us know what do you expect it to do, so we can modify it where needed and provide you with a working solution.

    Best wishes,
    Tsvetina
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017