babalae/better-genshin-impact · error · InvalidDataException
图片大小超过 {MaxDownloadBytes / 1024 / 1024} MB 限制。
Error message
图片大小超过 {MaxDownloadBytes / 1024 / 1024} MB 限制。 What it means
An InvalidDataException thrown by DownloadAndSaveAsync right after the response headers are read, when the declared Content-Length header exceeds MaxDownloadBytes. This is the early, cheap size guard that fails before streaming any body bytes. It protects against oversized banner images and memory/disk abuse.
Source
Thrown at BetterGenshinImpact/Service/BannerImageService.cs:95
using var timeoutCancellationTokenSource = new CancellationTokenSource(DownloadTimeout);
using var linkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(
cancellationToken,
timeoutCancellationTokenSource.Token);
var linkedCancellationToken = linkedCancellationTokenSource.Token;
try
{
// 下载图片
using var response = await _httpClient.GetAsync(
uri,
HttpCompletionOption.ResponseHeadersRead,
linkedCancellationToken).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
if (response.Content.Headers.ContentLength > MaxDownloadBytes)
{
throw new InvalidDataException($"图片大小超过 {MaxDownloadBytes / 1024 / 1024} MB 限制。");
}
await using (var responseStream = await response.Content
.ReadAsStreamAsync(linkedCancellationToken)
.ConfigureAwait(false))
await using (var fileStream = new FileStream(
tempPath,
FileMode.CreateNew,
FileAccess.Write,
FileShare.None,
81920,
FileOptions.Asynchronous | FileOptions.SequentialScan))
{
var buffer = new byte[81920];
long totalBytes = 0;
int read;
while ((read = await responseStream
.ReadAsync(buffer.AsMemory(), linkedCancellationToken)View on GitHub (pinned to a7cb36712d)
Solutions
- Use a smaller, web-optimized image (resize/compress the source to stay under the limit).
- Pick a different URL whose Content-Length is within MaxDownloadBytes.
- If the limit is genuinely too low for your use case, raise MaxDownloadBytes (review the trade-off first).
- Catch InvalidDataException and tell the user the image is too large.
Example fix
null
Defensive patterns
Strategy: try-catch
Validate before calling
// Issue a HEAD request first to read Content-Length without downloading the body
using var head = await _httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Head, uri), ct);
if (head.Content.Headers.ContentLength > MaxDownloadBytes) { /* reject early */ } Type guard
null
Try / catch
try { await _banner.DownloadAndSaveAsync(url, ct); }
catch (InvalidDataException ex) { Toast.Warning(ex.Message); } Prevention
- Use web-optimized images under MaxDownloadBytes.
- Pre-check size with a HEAD request when the source is user-supplied.
- Catch InvalidDataException and inform the user.
When it happens
Trigger: The server returns a response whose Content-Length header is greater than MaxDownloadBytes (the configured banner size limit). Triggered for legitimately large images or a wrong/abusive URL.
Common situations: Pointing the banner URL at a high-resolution original (tens of MB); an image host serving an uncompressed source; a misconfigured URL returning a large file; MaxDownloadBytes set lower than typical banner sizes.
Related errors
AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13).
Data as JSON: /api/errors/5250d7719e7c6ace.
Report an issue: GitHub.