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!
photoStringis the image encoded toUTF8, not to base 64.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 thatbuffercontains what you expect after callingblob.EndChunkWrite()? 3) Have you tried returning thebyte [] bufferarray directly? Most JSON serializers are able to serialize byte arrays natively, e.g. Json.NET serializes to base64.