I im trying to store GLTF file in a postgreSQL. I came up with an idea to convert file to byte array with memory Stream. But when i try to convert it back, from byte array to file with System.IO.File.WriteAllBytes i get damaged file.
Is it possible to convert gltf to bytes and then back again?
I was trying to use packages like SharpGLTF, but it does not help.
2
Answers
The trouble was that GLTF2.0 is encoded type of file. Converting it to array crashes its content. The only solution i found was using .obj or .fbx formats.
The basic approach to save or retrieve a file from the database is:
Read the raw file into a byte[] array.
Save that byte[] array into the database.
And to retrieve the file?
Then get/load/query the database row.
Cast the binary column into a byte[] array.
Save/write the byte array to a file.
So, code to save a file into database will look like this:
(I’m using SQL server, not PostgreSQL, but the code should be near identical using the PostgreSQL .net database provider).
So, to save a file to the database, then this:
And to do the reverse, and get a file from the database, then this:
I don’t know what column types are available in PostgreSQL, but a binary or blob type of column should exist, and using such a column type should allow the above process of converting a file to a byte[] array, and then sending that to the database.
And as the above shows, you reverse the process when wanting to get a valid file from the database. So, get the row you need, convert the blob/binary column to a byte array, and then save that byte array to a file.
Since we using a byte array, then the file type being text, pdf, a .dll, or whatever does not matter.
And I used in above some handy helper routines, since I don’t want to type over and over creating the database connection object, and command object.
So, the helper routines used above from my global static class was: