JeffreySu/WeiXinMPSDK · error · ArgumentException

文件名不能为空。

Error message

文件名不能为空。

What it means

ArgumentException raised by RequestMultipartCoreAsync when fileName is null, empty, or whitespace. Multipart file uploads to WeChat Pay (e.g. image/bill upload APIs) require a file name for the form-data part and signature meta.

Solutions

  1. Always supply a non-empty file name, e.g. Path.GetFileName(path) or an explicit name like "image.jpg"
  2. Guard at the caller: if (string.IsNullOrWhiteSpace(fileName)) throw before building the request
  3. Preserve the original filename through your upload pipeline

Example fix

// before
await apiRequest.RequestMultipartAsync(url, "", fileStream);
// after
var fileName = Path.GetFileName(localPath); // e.g. "logo.jpg"
await apiRequest.RequestMultipartAsync(url, fileName, fileStream);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(fileName))
    throw new ArgumentException("上传前需提供文件名", nameof(fileName));

Try / catch

try { await apiRequest.RequestMultipartAsync(url, fileName, fs); }
catch (ArgumentException ex) when (ex.ParamName == "fileName") { log.Error("文件名为空,无法上传", ex); throw; }

Prevention

When it happens

Trigger: Calling RequestMultipartAsync / RequestMultipartWithMaxSizeAsync / RequestMultipartWithFileDigestAsync / RequestMultipartWithFilenameAndFileDigestAsync with fileName = "" or null.

Common situations: Deriving the file name from Path.GetFileName on a path that was empty or a bare directory; a UI/upload layer that lost the original filename.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of JeffreySu/WeiXinMPSDK@be573f6f94 (2026-09-12). Data as JSON: /api/errors/255dce32ff4d71f0. Report an issue: GitHub.

Appendix: source

Thrown at src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/HttpHandlers/TenPayApiRequest.cs:378

            Func<T> createDefaultInstance = null,
            int maxFileBytes = 2 * 1024 * 1024)
            where T : ReturnJsonBase, new()
        {
            return RequestMultipartCoreAsync(url, fileName, fileStream,
                cancellationToken,
                MultipartMetaFieldStyle.FilenameAndFileDigest, timeOut,
                checkSign, createDefaultInstance, maxFileBytes);
        }

        private async Task<T> RequestMultipartCoreAsync<T>(string url, string fileName,
            Stream fileStream, CancellationToken cancellationToken,
            MultipartMetaFieldStyle metaFieldStyle, int timeOut, bool checkSign,
            Func<T> createDefaultInstance, int? maxFileBytes = null)
            where T : ReturnJsonBase, new()
        {
            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} 字节。");
            }

View on GitHub (pinned to be573f6f94)