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;
Mcfer
  • 194
  • 1
  • 4
  • 15

2 Answers2

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 ...
    }
}
Ralf Bönning
  • 14,515
  • 5
  • 49
  • 67
  • 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 ?? – Mcfer Sep 09 '16 at 15:50
  • Your code worked fine my friend...many thanks !! I just have the other question.. – Mcfer Sep 09 '16 at 15:50
  • 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. – Ralf Bönning Sep 09 '16 at 16:51
  • 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 – Mcfer Sep 09 '16 at 17:15
  • 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! – Ralf Bönning Sep 09 '16 at 17:20
  • I think you might be reading a little too deep into your clients encryption request. HTTPS:// is all you should need, just don't serve your content over HTTP:// make it unavailable that way or make the entire site HTTPS. You can get a 1yr free cert from https://startssl.com and renew every year for free. Or if you really want to encrypt you can do it, first encrypt in C# and decrypt on your client, if your using javascript take a look at this http://www.c-sharpcorner.com/UploadFile/4d9083/encrypt-in-javascript-and-decrypt-in-C-Sharp-with-aes-algorithm/ just do the reverse. – xer21 Sep 10 '16 at 02:53
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

Community
  • 1
  • 1
xer21
  • 304
  • 1
  • 11
  • 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. – Mcfer Sep 12 '16 at 15:00
  • @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: http://stackoverflow.com/questions/38962083/encrypt-json-result-on-webapi-backend/39463794#39463794 please mark as answer if it works out for you. – xer21 Sep 13 '16 at 06:50