1

I have a mini system where frontend will pass the file in base64 format to the backend. I used convert.frombase64string in order for me to convert the base64 format into byte array and used file stream to save the file into the server.

The codes are shown as below:

byte[] bytes = Convert.FromBase64String(file.Split(',')[1]);

using (var file = new FileStream("D:test.txt", FileMode.Create))
{
    file.Write(bytes, 0, bytes.Length);
    file.Flush();
}

var db = await _context.insertDB.FromSql("INSERT INTO blobTable (blob) VALUES ('" + bytes + "')").SingleAsync(); 

And this is the result that I select from blobTable.

enter image description here

As the return result from Convert.FromBase64String() is byte array. So that I decided to store this value into my database which is postgres with column bytea[].

The problem is that is it so weird that when I trying to

console.writeline(bytes);

The result is printed as "system.byte[]" instead of the bytes value. So that "system.byte[]" is stored into my database instead of the actual value.

Can anybody tell me how do I store the return value from convert.frombase64string() into postgres bytea[] column? Thanks you.

Presto
  • 888
  • 12
  • 30
Dave Cruise
  • 443
  • 7
  • 16
  • The problem is in your code that is writing to postgres, but you haven't showed us that code. It would be awesome if you could provide a [mcve]. – mjwills Jul 03 '18 at 04:28
  • 1
    `" + bytes + "` **is** converting it into a `string` - even if you don't want it to. Check out https://stackoverflow.com/a/7160845/34092 for how to pass byte arrays correctly. – mjwills Jul 03 '18 at 04:38
  • 2
    You should use paramterized SQL queries - it would solve your problem. – ProgrammingLlama Jul 03 '18 at 04:38

0 Answers0