Hi Kem,
this sounds interesting with Base64, but i can’t find any examples but with strings.
I need to encode other filetypes, i’m sure i can do this, but how can i decode them and open them again with the default app for this fileformat?
However you store the data in the database (BLOB or encoded string), you will also need to store the meta-data so you can properly restore it. At the least, that’s the file name.
To restore, you’d fetch the filename and data from the database, create a new empty file (BinaryStream.Create) with the right name, then write the data to that file.
If you want to restore the file exactly (same creation and modification date), and maybe save space too, you could:
Zip the file to a temporary file.
Read the zip file contents and store that to the database (again, BLOB or encoded string).
You would not need any other data about the zip file since it’s transitory.
To restore:
Read the data from the database.
Create a temp file and write the data to it.
Unzip the temp file to its final destination.
On Mac, you can use zip or ditto through a Shell to zip/unzip. On Linux, zip. On Windows, you can look for code on the forum that will tell Windows to do it for you. Or you can use MBS or Thomas Templeman’s zip package on all platforms.
Here is an example of a function to encode a pdf file into a string to be written to a blob record in a database:
Private Function PDFtoBlob(f as FolderItem) as String
Dim PdfContents as String =“”
dim bs as BinaryStream = BinaryStream.open(f)
dim sBlock as string = bs.Read(bs.Length)
bs.Close
PdfContents=EncodeBase64(sBlock)
sBlock=“”
Return PdfContents
End Function
Here is an example of a function to return the encoded string back to a pdf file
Private Sub BlobtoPDF(PdfContents as String, f as FolderItem)
Dim mb as MemoryBlock
mb=DecodeBase64(PdfContents)
dim bs as BinaryStream = BinaryStream.Create(f,True)
bs.Write(mb)
bs.Close
Don’t need to save a Zip file to store compressed blobs. …
There are undocumented classes in Xojo that are very unlikely to change (and should eventually be exposed): _gzip and _gunzip. Very handy.
I use them so i do not need to save temp files for DB blob type stuff…
I also use them with largish files my current app saves … They are JSON zipped in memory that I then store with a binary stream, with an app specific extension.