This is a migrated thread and some comments may be shown as answers.

can't catch error when binding an unsupported byte array

2 Answers 68 Views
BinaryImage
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 05 Jun 2015, 09:23 PM

Good day,

I'm currently migrating some user controls to telerik to use the new features. One of our user controls was a image upload control. Basically it had an asp.net img object and a stardard asp.net file upload object.

I changed it for a RadBinaryImage and a RadAsyncUpload. So far, the changes have worked. But I encountered a problem. Due to the lack of validation that the old control had, users could upload any file even if it wasn't images (like pdf, .doc, etc). Of course, it was not displayed properly on the asp.img object but it didn't show any exception, just an "x" sign. With the new user control, when I'm navigating through the records in a table and the control encounters one with a byte array that doesn't correspond to an image, it throws the following exception: "ArgumentException: The provided binary data may not be valid image or may contains unknown header".

I've read several posts in the forum and I already know that this exception is thrown because of the invalid format of the file loaded and that's ok, but I wanted to take control of this error if it happens so I did the following changes in the user control (ascx):

public void ShowImage(byte[] img)
{
    try
    {
        String strImageSessionName = String.Format("imagen{0}", imgMuestra.ClientID);
        Session[strImageSessionName] = img;
        imgMain.DataValue = img;
        imgTooltip.DataValue = img;
        imgMuestra.Style["cursor"] = "pointer";
        this.EnableTooltip = true;
    }
    catch (Exception)
    {
        imgMain.ImageUrl = "~/images/image_not_available.jpg";
        imgTooltip.ImageUrl = "~/images/image_not_available.jpg";
        this.EnableTooltip = false;
    }
     
}
 

 If there's any error when loading the bytes into the control I want to show instead a fixed image. The problem is that the catch block is never reached. It always do the try block entirely even if the data is in an incorrect format. The exception is thrown later in a uncontrolled manner by the form making the form crash.

What could I do to catch this error if it happens and achieve what I want? Thanks in advance for any help.

2 Answers, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 10 Jun 2015, 02:08 PM
Hello Jairo,

Could you create a new web site to demonstrate a very basic isolated version of your application? You can open a formal support ticket and send this runnable sample to us so we can debug and analyze the project locally.

Regards,
Eyup
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
David
Top achievements
Rank 1
answered on 10 Jun 2015, 02:13 PM

Hi,

I've created a support ticket. Thanks

Tags
BinaryImage
Asked by
David
Top achievements
Rank 1
Answers by
Eyup
Telerik team
David
Top achievements
Rank 1
Share this question
or