Image, file and upload fields
How to declare a photo, an avatar, a logo, a document or a gallery on a resource, what each one stores, and what you get in the admin without writing any of it.
The short answer
grit generate resource Product --fields "cover:file[image],gallery:files[image]"
file is one. files is many. The brackets say what the field accepts, and leaving them off accepts anything the server allows. That is the whole syntax; everything below is what it does.
What that writes
Copied from a real run, not paraphrased. The Go model:
Cover *files.FileRef `gorm:"type:json" json:"cover"`Gallery files.FileRefs `gorm:"type:json" json:"gallery"`
Note what it is not: a string holding a path. A FileRef is a JSON column carrying the key, the original filename, the size, the content type and the renditions, which is why the admin can show a thumbnail and a file size without a second request, and why deleting a record can delete its files.
And the admin field definition, which is where the accept list goes:
{ key: "cover", label: "Cover", type: "file", accepts: ["image"], maxSizeMB: 5 },{ key: "gallery", label: "Gallery", type: "files", accepts: ["image"], maxSizeMB: 5, max: 5 },
You get a dropzone for each, single or multiple, with drag and drop, a preview, a progress bar and the accept list wired in. max: 5 is the default cap on how many files a files field takes; change it in that line.
What it accepts
An alias, or a bracketed list of them. Both of these are the same field with a different vocabulary, and both parse:
cover:file[image] # the bracket form, used everywhere in these docscover:file:image # the colon form, also accepteddocs:files[pdf, doc] # a list. Spaces after the commas are fineattachment:file # no brackets: anything the server allows
| Alias | Lets through |
|---|---|
| image | jpg, jpeg, png, gif, webp, avif, svg |
| video | the common web video container types |
| audio | the common web audio types |
| application/pdf | |
| doc | Word, Excel, PowerPoint, CSV and plain text |
| all | anything the server allows. The same as leaving the brackets off. |
The accept list is not only a browser hint
This is the part worth knowing, because in most stacks it is not true. The dropzone uses the list to filter the file picker, and it also travels to the server:
POST /api/uploads?accepts=image&max_size=5242880
The upload handler resolves those aliases itself through internal/files/accepts.go and refuses anything outside them, so a client that edits the request, or one that never used your form, gets the same answer. The size cap is enforced in the same place.
UPLOAD_ALLOWED_MIME in .env is the outer boundary for the whole project. A field's accept list can narrow it and cannot widen it.
Images get more than storage
An upload that is an image goes through the processing pipeline on the way in, with no configuration: it is resized, re-encoded and stripped of its EXIF, which matters more than it sounds because a phone photo carries the GPS coordinates of where it was taken. The renditions land in the same FileRef. Media and image processing has the profiles and the measured numbers.
Where the bytes go
Wherever STORAGE_DRIVER says: local needs nothing at all, minio is what docker compose up starts, and s3, r2 and b2 need credentials. The field declaration is the same in every case, which is the point of the FileRef being a reference rather than a path. File storage covers the drivers.
Three things that catch people
- A file field is nullable and a files field is not.
*files.FileRefcan benull;files.FileRefsis an empty list when there is nothing in it. Write your frontend against that rather than againstundefined. - Marking it required does not mean the upload succeeded. The form sends a reference, and the file arrived through a separate request before that. If you need the two to be atomic, check the reference in your service.
- Renaming the field does not move the files. The key stored in the
FileRefis where the object actually is. Changing the column name changes nothing in the bucket, which is usually what you want.
Every other field type
This page is only about files because it is the question asked most. The field types reference has all thirty-two, with the Go type, the TypeScript type, the Zod schema and the admin control each one produces.
