JeffreySu/WeiXinMPSDK · error · ArgumentException
文件名不能为空。
Error message
文件名不能为空。
What it means
ValidateImageFileName (ProductCouponApis.cs:809), used by UploadImageAsync, guards the WeChat product-coupon image upload API: the file name must be non-empty (and its extension must be jpg/png/jpeg etc.). An empty/whitespace fileName throws ArgumentException naming the parameter before any HTTP request is made.
Solutions
- Ensure a non-empty file path/name is passed to UploadImageAsync and check it before calling
- Verify the file exists and Path.GetFileName(path) yields a value with a supported extension (.jpg/.png/.jpeg)
- Add an upfront guard in your upload flow to reject empty file names with a friendly error
Example fix
// before
await api.UploadImageAsync(fileName); // fileName == ""
// after
if (string.IsNullOrWhiteSpace(fileName))
throw new InvalidOperationException("Image file name must be provided");
await api.UploadImageAsync(fileName); Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrWhiteSpace(fileName))
throw new ArgumentException("Image file name is required", nameof(fileName));
var ext = Path.GetExtension(fileName)?.ToLowerInvariant();
if (ext is not (".jpg" or ".jpeg" or ".png"))
throw new ArgumentException($"Unsupported image extension: {ext}"); Type guard
bool ValidImageName(string? fileName) =>
!string.IsNullOrWhiteSpace(fileName) &&
new[] { ".jpg", ".jpeg", ".png" }.Contains(Path.GetExtension(fileName)?.ToLowerInvariant()); Try / catch
try { await api.UploadImageAsync(fileName); }
catch (ArgumentException ex) when (ex.Message.Contains("文件名不能为空"))
{ logger.LogError(ex, "Empty file name passed to coupon image upload"); throw; } Prevention
- Validate file name/extension at the UI or API boundary before invoking the SDK
- Ensure file pickers always supply a name; never forward raw empty config values
- Run Path.GetFileName on full paths to guarantee a non-empty name
When it happens
Trigger: Calling UploadImageAsync with a null, empty, or whitespace-only fileName string, typically because the path variable was never assigned or an upload form field was missing.
Common situations: Reading a file name from user input/config that came back empty; passing Path.GetFileName on a path that had no filename; upload pipeline dropping the file name before reaching the API call.
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/b7a3e32e3e8f9647.
Report an issue: GitHub.
Appendix: source
Thrown at src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/Apis/ProductCoupon/ProductCouponApis.cs:809
Escape(query[index + 1]));
}
}
return parts.Count == 0
? path
: $"{path}?{string.Join("&", parts)}";
}
private static string Escape(string value)
{
return Uri.EscapeDataString(value ?? string.Empty);
}
private static void ValidateImageFileName(string fileName)
{
if (string.IsNullOrWhiteSpace(fileName))
{
throw new ArgumentException("文件名不能为空。",
nameof(fileName));
}
switch (Path.GetExtension(fileName)?.ToLowerInvariant())
{
case ".jpg":
case ".jpeg":
case ".bmp":
case ".png":
return;
default:
throw new ArgumentException(
"商品券图片仅支持 JPG、JPEG、BMP 或 PNG。",
nameof(fileName));
}
}
}
}View on GitHub (pinned to be573f6f94)