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.

3
  • 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 minimal reproducible example. Commented Jul 3, 2018 at 4:28
  • 1
    " + bytes + " is converting it into a string - even if you don't want it to. Check out stackoverflow.com/a/7160845/34092 for how to pass byte arrays correctly. Commented Jul 3, 2018 at 4:38
  • 2
    You should use paramterized SQL queries - it would solve your problem. Commented Jul 3, 2018 at 4:38

0

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.