How to display the filename of an image in a gallery field in Sanity.io
Good news—you can definitely show the filename! The key is placing the preview configuration on the image object itself, not on the array field.
When you add custom fields to an image type in an array, Sanity stops showing the default filename preview. You need to explicitly tell it what to display by adding a preview configuration. Here's how:
{
name: 'gallery',
type: 'array',
of: [
{
type: 'image',
fields: [
{
name: 'alt',
type: 'string',
title: 'Alt Text'
}
],
preview: {
select: {
title: 'asset.originalFilename',
media: 'asset'
}
}
}
]
}The important bits:
title: 'asset.originalFilename'- This pulls the original filename from the image assetmedia: 'asset'- This keeps the image thumbnail visible in the list
The asset object is automatically populated when you upload an image and includes read-only properties like originalFilename, url, size, etc. You don't need to create any additional fields—just reference asset.originalFilename in your preview select.
If you want to get fancier, you can use a prepare function to customize the display even further, but for showing filenames, the simple select approach above should work perfectly.
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.