0

So, i am getting the byte array of a LongRaw image from Oracle... I am using a webapi to this. After get the array, how i use it on the Client-side ? Do Its better i convert to base64string and pass this value converting just at the client side ?

cmd.InitialLONGFetchSize = -1;
                var reader = cmd.ExecuteReader();

                if (reader.Read())
                {
                    // Fetch the LONG RAW
                    OracleBinary imgBinary = reader.GetOracleBinary(0);
                    // Get the bytes from the binary obj
                    byte[] imgBytes = imgBinary.IsNull ? null : imgBinary.Value;

                    //var imgString = Uri.EscapeDataString(Convert.ToBase64String(imgBytes));
                }

                //CRIO A LISTA
                lretorno.Load(reader, LoadOption.OverwriteChanges, "BUSCAFOTO");

                reader.Close();

                connection.Close();
                connection.Dispose();

                var teste = lretorno.Tables[0].AsEnumerable().Select(row => new FotoEnvolvido
                {
                    FOTO = (byte[])(row["FOTO"]),
                    //FOTO = Convert.ToString(row["FOTO"]),
                });

                return teste;

2 Answers 2

2

You can write a Web API Controller that returns the binary data of an image. Base64 strings impose a overhead of the amount of bytes that have to be transmitted. Please avoid this.

A sample controller can look like this example:

public class WebApiController : ApiController
{        
    public async Task<HttpResponseMessage> Get(string id)
    {
        var bytes = await GetBytesFromDataLayerAsync(id);

        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
        var stream = new MemoryStream(bytes);
        result.Content = new StreamContent(stream);
        result.Content.Headers.ContentType =
            new MediaTypeHeaderValue("image/jpeg");
        return result;
    }

    private async Task<byte[]> GetBytesFromDataLayerAsync(string id)
    {
        // put your Oracle logic here
        return ...
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

I need to encrypt the content of my Json´s... i did this to all strings...But, now with these ByteArray, i think its better i convert before pass the image, is not ??
Your code worked fine my friend...many thanks !! I just have the other question..
Why do you need to encrypt the content of your JSONs? Normally they are protected by SSL/TLS.... https... - But if you have another question regarding encryption/decryption you should post a new question on SO.
Oh men... i don´t know !! I just know that i need...lol I already told to them, but the security department are not normal
Oh dear - I cannot even imagine a solution. The browser has to decrypt the JSON data (this could be done by transferring an encrypted Json-String + encryption on the JavaScript side (this code can be accessed by anyone). Perhaps there are some good JavaScript libraries. But this is a whole new topic (and another post). Good Luck!
|
1

Depending on what your doing as rboe said writing the bytes directly to the client will save some data size(approx. 37%) and computing overhead. If your not only displaying jpeg images you should also set the mime-type to the correct value... take a look at this source for a rather complete set of extension to mime-type mappings. If you do not know the mime-type you can try "application/octet-stream" as that is the general mime-type for binary data.

If your displaying your content via web browser you could just use an <img> tag something like <img src="view_image.aspx?id=5"> you can even create the dynamically with javascript/jQuery.

If you really do want the image data embedded in a json request which might be useful if you have a lot of little icons and don't want a ton of requests (with http/2 I don't think this will matter) or another reason, then yes first encode the binary data using...

string base64EncodedData = Convert.ToBase64String(bytes);

If the client is javascript you can decode using the latest browsers native functions

var decodedImageData = window.atob(base64EncodedData);

See:

If you are however just sending it to another c# endpoint you can use...

byte[] decodedImageData = Convert.FromBase64String(base64EncodedData);

And like I mentioned in the comment to ensure it's encrypted just make the site only support https:// and if you don't have a SSL cert you can get one for free from http://startssl.com

2 Comments

My friend...thanks for the explanation !! Awsome.... But, i already told to everybody here in my job about this (ssl "encrypt") and doesnt matter... all the protocols are over HTTPS, but they still need that i encrypt the datas. Oh, i am using xamarin c# to catch the datas from web api.
@MarceloCFernandes Since you still needed help with encryption, I looked up your other encrypt json question and posted an answer on there. You can use the same code basically for encrypting the image in addition to the json See: stackoverflow.com/questions/38962083/… please mark as answer if it works out for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.