fullstackhero/dotnet-starter-kit · error · InvalidOperationException
File exceeds max size of
Error message
File exceeds max size of {rules.MaxSizeInMB} MB. What it means
LocalStorageService.UploadAsync enforces the per-FileType size cap rules.MaxSizeInMB by checking request.Data.Count against the limit in bytes. Files larger than the cap throw InvalidOperationException before the blob is written to disk. This protects local disk from oversized payloads.
Solutions
- Upload a smaller file or compress/downscale before upload.
- Raise rules.MaxSizeInMB for the FileType in FileTypeMetadata if the cap is too strict.
- Chunk or stream large files instead of a single UploadAsync call.
- Check file size client-side and block the upload before it starts.
Example fix
// before: no size check
await storage.UploadAsync(new UploadRequest { FileName = name, FileType = FileType.Video, Data = hugeFile });
// after
const long maxBytes = 50 * 1024 * 1024;
if (hugeFile.Length > maxBytes) return BadRequest("Video exceeds 50 MB limit");
await storage.UploadAsync(new UploadRequest { FileName = name, FileType = FileType.Video, Data = hugeFile }); Defensive patterns
Strategy: validation
Validate before calling
var rules = FileTypeMetadata.GetRules(fileType);
var maxBytes = (long)rules.MaxSizeInMB * 1024 * 1024;
if (data.Length > maxBytes)
throw new ArgumentException($"File exceeds max size of {rules.MaxSizeInMB} MB."); Type guard
bool IsWithinSizeLimit(long byteCount, FileTypeRules rules) => byteCount <= (long)rules.MaxSizeInMB * 1024 * 1024;
Try / catch
try {
await storage.UploadAsync(request);
} catch (InvalidOperationException ex) when (ex.Message.Contains("exceeds max size")) {
return Results.StatusCode(413);
} Prevention
- Enforce the same MaxSizeInMB limit client-side before upload.
- Compress images/video or split large imports into chunks.
- Document size limits per file type in API docs.
When it happens
Trigger: UploadAsync called with request.Data whose byte count exceeds rules.MaxSizeInMB * 1024 * 1024 for the given FileType.
Common situations: Users uploading large videos or high-resolution images; batch/import tools streaming whole files into memory; MaxSizeInMB tightened in config without updating client-side limits; frontend not enforcing a pre-upload size check.
Understand the failure class
Background: "File too large" / "file size exceeds limit" errors: why libraries cap file sizes and how to fix them — this error's family across 46 libraries.
Related errors
- File exceeds max size of
- uploaded size ( ) exceeds declared ( )
- Storage quota exceeded
- File type ' ' is not allowed. Allowed
- File type ' ' is not allowed. Allowed
AI-assisted analysis of fullstackhero/dotnet-starter-kit@3f2959e683 (2026-09-15).
Data as JSON: /api/errors/4c4ee4a130596f5c.
Report an issue: GitHub.
Appendix: source
Thrown at src/BuildingBlocks/Storage/Local/LocalStorageService.cs:49
}
public async Task<string> UploadAsync<T>(FileUploadRequest request, FileType fileType, CancellationToken cancellationToken = default)
where T : class
{
ArgumentNullException.ThrowIfNull(request);
var rules = FileTypeMetadata.GetRules(fileType);
var extension = Path.GetExtension(request.FileName);
if (string.IsNullOrWhiteSpace(extension) ||
!rules.AllowedExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase))
{
throw new InvalidOperationException($"File type '{extension}' is not allowed. Allowed: {string.Join(", ", rules.AllowedExtensions)}");
}
if (request.Data.Count > rules.MaxSizeInMB * 1024 * 1024)
{
throw new InvalidOperationException($"File exceeds max size of {rules.MaxSizeInMB} MB.");
}
#pragma warning disable CA1308 // folder names are intentionally lower-case for URLs/paths
var folder = FolderSanitizer().Replace(typeof(T).Name.ToLowerInvariant(), "_");
#pragma warning restore CA1308
var safeFileName = $"{Guid.NewGuid():N}_{SanitizeFileName(request.FileName)}";
var relativePath = Path.Combine(UploadBasePath, folder, safeFileName);
var fullPath = Path.Combine(_rootPath, relativePath);
Directory.CreateDirectory(Path.GetDirectoryName(fullPath)!);
await File.WriteAllBytesAsync(fullPath, request.Data.ToArray(), cancellationToken);
return relativePath.Replace("\\", "/", StringComparison.Ordinal); // Normalize for URLs
}
public Task<FileDownloadResponse?> DownloadAsync(string path, CancellationToken cancellationToken = default)
{View on GitHub (pinned to 3f2959e683)