{"record":{"id":"cc3ceb3128177942","repo":"JeffreySu/WeiXinMPSDK","slug":"maxfilebytes-value","errorCode":null,"errorMessage":"上传文件超过允许上限 {maxFileBytes.Value} 字节。","messagePattern":"上传文件超过允许上限 (.+?) 字节。","errorType":"exception","errorClass":"InvalidDataException","httpStatus":null,"severity":"error","filePath":"src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/HttpHandlers/TenPayApiRequest.cs","lineNumber":394,"sourceCode":"            if (string.IsNullOrWhiteSpace(fileName))\n            {\n                throw new ArgumentException(\"文件名不能为空。\", nameof(fileName));\n            }\n\n            _ = fileStream ?? throw new ArgumentNullException(nameof(fileStream));\n            if (timeOut <= 0 && timeOut != Timeout.Infinite)\n            {\n                throw new ArgumentOutOfRangeException(nameof(timeOut), \"超时时间必须大于 0，或使用 Timeout.Infinite。 \");\n            }\n            if (maxFileBytes.HasValue && maxFileBytes.Value <= 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(maxFileBytes),\n                    \"文件大小上限必须大于 0。\");\n            }\n            if (maxFileBytes.HasValue && fileStream.CanSeek &&\n                fileStream.Length - fileStream.Position > maxFileBytes.Value)\n            {\n                throw new InvalidDataException(\n                    $\"上传文件超过允许上限 {maxFileBytes.Value} 字节。\");\n            }\n\n            T result = null;\n            try\n            {\n                byte[] fileBytes;\n                int fileLength;\n                using (var memoryStream = new MemoryStream())\n                {\n                    var buffer = ArrayPool<byte>.Shared.Rent(81920);\n                    try\n                    {\n                        int bytesRead;\n                        while ((bytesRead = await fileStream.ReadAsync(buffer,\n                            0, buffer.Length, cancellationToken)\n                            .ConfigureAwait(false)) > 0)\n                        {","sourceCodeStart":376,"sourceCodeEnd":412,"githubUrl":"https://github.com/JeffreySu/WeiXinMPSDK/blob/be573f6f94bdbf718dd5f6cdecb137fbc7ff651e/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/HttpHandlers/TenPayApiRequest.cs#L376-L412","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"// before\nawait apiRequest.RequestMultipartWithMaxSizeAsync(url, json, fs, maxFileBytes: 1_000_000);\n// after\nif (fs.Length - fs.Position <= 10 * 1024 * 1024)\n{\n    await apiRequest.RequestMultipartWithMaxSizeAsync(url, json, fs, maxFileBytes: 10 * 1024 * 1024);\n}","handlingStrategy":"validation","validationCode":"if (fileStream.CanSeek && maxFileBytes.HasValue && fileStream.Length - fileStream.Position > maxFileBytes.Value)\n    throw new InvalidOperationException($\"File exceeds {maxFileBytes.Value} bytes limit.\");","typeGuard":"bool WithinLimit(Stream s, long? max) => !max.HasValue || !s.CanSeek || s.Length - s.Position <= max.Value;","tryCatchPattern":"try { /* multipart upload */ }\ncatch (InvalidDataException ex) { logger.LogWarning(ex, \"Upload rejected: file too large\"); return Results.PayloadTooLarge(); }","preventionTips":["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."],"tags":["file-size","upload","validation"],"backgroundTag":"file-size-limit-exceeded","analyzedSha":"be573f6f94bdbf718dd5f6cdecb137fbc7ff651e","analyzedAt":"2026-09-12T10:01:50.733Z","contentChangedAt":"2026-09-12T10:01:50.733Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}