[B4X] PocketBase - Open Source backend in 1 file
PocketBase was created to assist building self-contained applications that can run on a single server without requiring to install anything in addition. The basic idea is that the common functionality like crud, auth, files upload, auto TLS, etc. are handled out of the box, allowing you to focus...
www.b4x.com
Please read the official documentation about storage in pocketbase:
PocketBase - Open Source backend in 1 file
Open Source backend in 1 file with realtime database, authentication, file storage and admin dashboard
pocketbase.io
Uploading files
To upload files, you must first add a file field to your collection
You can upload one or more files at the same time. If a file field allows several files then also to the same column.
Required parameters:
- CollectionName
- dt_Task
- RecordId
- s64f723suu7b1p4
B4X:
Dim lst_Files As List : lst_Files.Initialize
lst_Files.Add(Pocketbase_Functions.CreateMultipartFileData(File.DirAssets,"test.jpg","Task_Image",""))
lst_Files.Add(Pocketbase_Functions.CreateMultipartFileData(File.DirAssets,"test2.jpg","Task_Image",""))
Wait For (xPocketbase.Storage.UploadFiles("dt_Task","s64f723suu7b1p4",lst_Files)) Complete (DatabaseResult As PocketbaseDatabaseResult)
xPocketbase.Database.PrintTable(DatabaseResult)
Single file:
B4X:
Wait For (xPocketbase.Storage.UploadFile("dt_Task","s64f723suu7b1p4",Pocketbase_Functions.CreateMultipartFileData(File.DirAssets,"test.jpg","Task_Image",""))) Complete (DatabaseResult As PocketbaseDatabaseResult)
xPocketbase.Database.PrintTable(DatabaseResult)
If you only want to upload one file, then it doesn't matter which function you use, making 2 out of it was just a nice to have.
Downloading files
Required parameters:
- CollectionName
- dt_Task
- RecordId
- s64f723suu7b1p4
- FileName
- test_76uuo6rx0u.jpg
B4X:
Wait For (xPocketbase.Storage.DownloadFile("dt_Task","s64f723suu7b1p4","test_76uuo6rx0u.jpg")) Complete (StorageFile As PocketbaseStorageFile)
If StorageFile.Error.Success Then
Log($"File ${"test.jpg"} successfully downloaded "$)
ImageView1.SetBitmap(Pocketbase_Functions.BytesToImage(StorageFile.FileBody))
Else
Log("Error: " & StorageFile.Error.ErrorMessage)
End If
Helper functions
I provide a few useful functions that make it even easier to upload a file.
CreateMultipartFileData
creates a MultipartFileData object for file uploads, automatically determining the MIME type if ContentType is empty.
B4X:
Pocketbase_Functions.CreateMultipartFileData(File.DirAssets,"test.jpg","Task_Image",""))
GetMimeTypeByExtension
returns the MIME type based on the file extension, categorizing images, videos, and audio formats. Logs a warning for unknown types.
B4X:
Log(Pocketbase_Functions.GetMimeTypeByExtension(".mp4"))
GetFileExt
returns the file extension from a given filename, including the leading dot (.)
B4X:
Log(Pocketbase_Functions.GetFileExt("TestFile.mp4"))
MultipartFileData
This is the object that is passed to upload a file, you can also use the
Pocketbase_Functions.CreateMultipartFileData(File.DirAssets, “test.jpg”, “Task_Image”,“”))
function.
B4X:
Dim FileData As MultipartFileData
FileData.Initialize
FileData.Dir = File.DirAssets
FileData.FileName = "test.jpg"
FileData.KeyName = "Task_Image" 'File Column Name
FileData.ContentType = "image/png" 'You can use the Pocketbase_Functions.GetMimeTypeByExtension function