Upload files to PocketBase records using the file field type
PocketBase handles file uploads through the file field type. When you create or update a record with file fields, you can upload new files, manage existing ones, and delete unwanted files.
// Single file - stored as string in databasefield := &core.FileField{ Name: "avatar", MaxSelect: 1,}// Upload single filerecord.Set("avatar", file)// The last file wins if you set multiple timesrecord.Set("avatar", file1)record.Set("avatar", file2) // file2 overwrites file1
When you remove files using the - operator or by replacing the field value, the actual files are deleted from storage after the record is successfully saved.
field := &core.FileField{ Name: "upload", MaxSize: 2 << 20, // 2MB limit}// This will fail if file is larger than 2MBrecord.Set("upload", largeFile)err := app.Save(record) // Returns validation error
The default maximum file size is 5MB. You can set MaxSize to 0 to use the default, or specify a custom value up to 2^53-1 bytes.
field := &core.FileField{ Name: "image", MimeTypes: []string{ "image/png", "image/jpeg", "image/webp", },}// This will fail if file is not PNG, JPEG, or WebPrecord.Set("image", pdfFile)err := app.Save(record) // Returns validation error
field := &core.FileField{ Name: "gallery", MaxSelect: 3,}// This will fail - too many filesrecord.Set("gallery", []*filesystem.File{f1, f2, f3, f4})err := app.Save(record) // Returns "too many files" error
When using direct filesystem uploads, you’re responsible for managing file paths, cleanup, and access control. For most use cases, use the file field type instead.
File upload errors are handled during record validation and save:
record.Set("avatar", file)err := app.Save(record)if err != nil { // Check for specific validation errors if strings.Contains(err.Error(), "validation_invalid_file") { // Invalid file format or type } if strings.Contains(err.Error(), "validation_too_many_files") { // Exceeds MaxSelect } if strings.Contains(err.Error(), "UploadedFileSize") { // File too large } if strings.Contains(err.Error(), "UploadedFileMimeType") { // Invalid MIME type } return err}