Images
Upload images, and store a reference to them
Using the image editor
The image editor allows a user to upload an image, and stores a reference to the image in a string property, such as a file name, a storage key or a URL. The image editor also allows users to upload files by dropping it on the editor.


To use the image editor, call UseImageEditor() and pass StoreImage() a function that saves the file.
using StellarAdmin.Resources.Editors;
fields.Add(destination => destination.HeroImageUrl).UseImageEditor(image =>
{
image.StoreImage((file, services, cancellationToken) =>
services.GetRequiredService<IPhotoStore>().SaveAsync(file, cancellationToken));
});When someone uploads an image, StellarAdmin checks the file's size and type, and then calls the StoreImage() function with the uploaded IFormFile. The function must save the file and return a string, which StellarAdmin stores in the property when the form is saved. The function must not return null, an empty string or whitespace, since an empty value means "no image". If it does, the upload fails with a server error, and the editor displays a message that the file couldn't be uploaded.
The example below saves the photo in the app's wwwroot/photos folder, and returns the file name:
public class PhotoStore(IWebHostEnvironment environment) : IPhotoStore
{
public async Task<string> SaveAsync(IFormFile file, CancellationToken cancellationToken)
{
var fileName = $"{Guid.NewGuid()}{Path.GetExtension(file.FileName)}";
var path = Path.Combine(environment.WebRootPath, "photos", fileName);
await using var stream = File.Create(path);
await file.CopyToAsync(stream, cancellationToken);
return fileName;
}
}You will typically not store the full URL needed to display the image in the browser, but rather just something like a filename. You can specify a ResolveUrl() function that turns the property value into a proper URL the browser can display.
using StellarAdmin.Resources.Editors;
fields.Add(destination => destination.HeroImageUrl).UseImageEditor(image =>
{
image.StoreImage(...);
image.ResolveUrl(value => $"/photos/{value}");
});To build the URL with a service registered with dependency injection, pass a function that also takes the services and a cancellation token: ResolveUrl(async (value, services, cancellationToken) => ...).
To display the image in the grid or on the view page, use the ImageDisplay display type, with the same ResolveUrl() function.
You must specify a StoreImage() handler for your image editor. Without it, StellarAdmin leaves the field off the form, and reports a configuration problem.
Changing the editor's settings
The example below uses the compact layout for a guide's photo, and accepts files up to 10 MB:
fields.Add(guide => guide.PhotoUrl).UseImageEditor(image =>
{
image.StoreImage((file, services, cancellationToken) =>
services.GetRequiredService<IPhotoStore>().SaveAsync(file, cancellationToken));
image.Layout = ImageEditorLayout.Compact;
image.MaxFileSize = 10 * 1024 * 1024;
});| Setting | What it does |
|---|---|
Layout | Sets how the editor is laid out: ImageEditorLayout.Dropzone, a drop zone that displays the image with replace and remove buttons over it; Compact, a thumbnail beside upload and remove buttons; or Attachment, a row with the thumbnail and the file's name. The default is Dropzone. |
MaxFileSize | Sets the largest file the editor accepts, in bytes. The default is 5 MB. |
AcceptedTypes | Sets the media types the editor accepts, such as image/png. The default is JPEG, PNG, WebP, GIF and AVIF. To accept SVG files, add image/svg+xml. |






In an inline row, the image editor is a thumbnail button. If someone saves the row while an image is uploading, StellarAdmin asks them to wait for the upload, and doesn't save the row.


Deleting images
StellarAdmin never deletes images. When a user removes an image using the image editor, StellarAdmin only clears the value of the field. The underlying image is not deleted. Likewise, when a user replaces an image with a new one - the previous image is not deleted. If you want to delete unused images, you must write your own logic to do this on a periodic basis.