JeffreySu/WeiXinMPSDK · error · InvalidDataException
上传文件超过允许上限 字节。
Error message
上传文件超过允许上限 {maxFileBytes.Value} 字节。 What it means
Before buffering the multipart upload, RequestMultipartCoreAsync checks whether the seekable fileStream's remaining length (Length - Position) exceeds the configured maxFileBytes and throws InvalidDataException if so. This is an early pre-flight size check so oversized uploads are rejected before any bytes are sent.
Solutions
- Increase maxFileBytes to accommodate the actual file size (respecting WeChat Pay's per-media-type limits).
- Validate fileStream.Length before calling and reject oversized files in your own UI/API with a friendly message.
- If the stream was partially consumed, reset Position or pass a fresh stream so the remaining-length check measures the real content.
- If the file is not required to be this large, compress or re-export the asset under the limit.
Example fix
// before
await apiRequest.RequestMultipartWithMaxSizeAsync(url, json, fs, maxFileBytes: 1_000_000);
// after
if (fs.Length - fs.Position <= 10 * 1024 * 1024)
{
await apiRequest.RequestMultipartWithMaxSizeAsync(url, json, fs, maxFileBytes: 10 * 1024 * 1024);
} Defensive patterns
Strategy: validation
Validate before calling
if (fileStream.CanSeek && maxFileBytes.HasValue && fileStream.Length - fileStream.Position > maxFileBytes.Value)
throw new InvalidOperationException($"File exceeds {maxFileBytes.Value} bytes limit."); Type guard
bool WithinLimit(Stream s, long? max) => !max.HasValue || !s.CanSeek || s.Length - s.Position <= max.Value;
Try / catch
try { /* multipart upload */ }
catch (InvalidDataException ex) { logger.LogWarning(ex, "Upload rejected: file too large"); return Results.PayloadTooLarge(); } Prevention
- Check file size at ingress (HTML maxlength, server-side validation) before forwarding to WeChat Pay.
- Set limits matching WeChat Pay's documented per-media-type maximums.
- Reset stream Position before upload if the stream was previously read.
When it happens
Trigger: Calling any RequestMultipart* overload with a seekable FileStream whose remaining content is larger than maxFileBytes (e.g. a 12 MB certificate image when the limit is 10 MB).
Common situations: Uploading WeChat Pay media (brand images, certificates, complaint files) that grew past a hard-coded limit; limit configured too low for production files; users uploading large media through an app that forwards it to WeChat Pay.
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
AI-assisted analysis of JeffreySu/WeiXinMPSDK@be573f6f94 (2026-09-12).
Data as JSON: /api/errors/cc3ceb3128177942.
Report an issue: GitHub.
Appendix: source
Thrown at src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/HttpHandlers/TenPayApiRequest.cs:394
if (string.IsNullOrWhiteSpace(fileName))
{
throw new ArgumentException("文件名不能为空。", nameof(fileName));
}
_ = fileStream ?? throw new ArgumentNullException(nameof(fileStream));
if (timeOut <= 0 && timeOut != Timeout.Infinite)
{
throw new ArgumentOutOfRangeException(nameof(timeOut), "超时时间必须大于 0,或使用 Timeout.Infinite。 ");
}
if (maxFileBytes.HasValue && maxFileBytes.Value <= 0)
{
throw new ArgumentOutOfRangeException(nameof(maxFileBytes),
"文件大小上限必须大于 0。");
}
if (maxFileBytes.HasValue && fileStream.CanSeek &&
fileStream.Length - fileStream.Position > maxFileBytes.Value)
{
throw new InvalidDataException(
$"上传文件超过允许上限 {maxFileBytes.Value} 字节。");
}
T result = null;
try
{
byte[] fileBytes;
int fileLength;
using (var memoryStream = new MemoryStream())
{
var buffer = ArrayPool<byte>.Shared.Rent(81920);
try
{
int bytesRead;
while ((bytesRead = await fileStream.ReadAsync(buffer,
0, buffer.Length, cancellationToken)
.ConfigureAwait(false)) > 0)
{View on GitHub (pinned to be573f6f94)