1

I've written an ASP.NET web service that takes requests, gathers data from an Oracle Database and sends back the information via JSON. The data includes an image file (jpg) that needs to be returned in Base64.

All was going well until I tried to return the image. My code is as follows:

Oracle Stored Procedure: (paraphrased)

create or replace PROCEDURE check_data(
<in parameters here>,
outpixblob      OUT BLOB);

fileptr := utl_file.fopen('PIX_DIR', vFilename, 'rb', 32760);
utl_file.get_raw(fileptr, outpixblob);

This seems to work well, in that I've traced through it and am seeing predictable data returned (base64 string)

Now, to the C# code:

conn.Open();
            var cmd = new OracleCommand
            {
                Connection = conn,
                CommandText = "CHECK_DATA",
                CommandType = CommandType.StoredProcedure,
            };
            //lots of parameter settings here, just showing the pertinent one
            //cmd.Parameters.Add("outpixblob", OracleDbType.Blob).Direction = ParameterDirection.Output;
            var dr = cmd.ExecuteNonQuery();

            var blob = (OracleBlob) cmd.Parameters["outpixblob"].Value;

            var buffer = new byte[blob.Length];
            blob.BeginChunkWrite();
            blob.Write(buffer, 0, (int)blob.Length);
            blob.EndChunkWrite();
            string photoString = System.Text.Encoding.UTF8.GetString(buffer, 0, buffer.Length);


            result = new VisitorCheckResult
            {
                ...
                Photo     =     photoString,

            };

I've obviously cut out non-essential code again, but I believe the idea of what I'm doing is there.

The result of photostring is not the values I saw when I debugged the Oracle sproc; instead, it is a long string of '\u0000' over and over again.

I'm sure there's something obvious I'm missing, but being a n00b at this I'm not able to see what it is.

Help!

5
  • photoString is the image encoded to UTF8, not to base 64. Commented Mar 21, 2016 at 19:07
  • what encoding should I use instead? Commented Mar 21, 2016 at 19:09
  • Have you tried System.Convert.FromBase64String() or System.Convert.FromBase64CharArray() ? Commented Mar 21, 2016 at 19:23
  • I did, and the result I got was a long string of 'AAAAAAAAA' ad infinitum. Commented Mar 21, 2016 at 19:26
  • 1
    1) You cannot encode arbitrary binary data using UTF8.GetString(). bytes that are not valid Unicode UTF-8 characters will be replaced with a "fallback character", see Choosing a Fallback Strategy. 2) Have you verified that buffer contains what you expect after calling blob.EndChunkWrite()? 3) Have you tried returning the byte [] buffer array directly? Most JSON serializers are able to serialize byte arrays natively, e.g. Json.NET serializes to base64. Commented Mar 21, 2016 at 20:04

1 Answer 1

2

Ok, found the answer. Turns out I was using the write() method with the incorrect idea that it would write to the buffer. The Write() method writes data TO the Oracle BLOB object. I needed instead to use the Read() Method.

Once that happened, and after I converted the byte[] array to base64:

result.Photo = Convert.ToBase64String(buffer);

The result came out beautifully.

Thanks to all, and especially thanks to dbc, whose response helped me realize what was going on.

Sign up to request clarification or add additional context in comments.

Comments

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.