{"record":{"id":"09b59f2d6590916b","repo":"JeffreySu/WeiXinMPSDK","slug":"jpg-jpeg-bmp-png-productcouponapis","errorCode":null,"errorMessage":"商品券图片仅支持 JPG、JPEG、BMP 或 PNG。","messagePattern":"商品券图片仅支持 JPG、JPEG、BMP 或 PNG。","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/Apis/ProductCoupon/ProductCouponApis.cs","lineNumber":821,"sourceCode":"        }\n\n        private static void ValidateImageFileName(string fileName)\n        {\n            if (string.IsNullOrWhiteSpace(fileName))\n            {\n                throw new ArgumentException(\"文件名不能为空。\",\n                    nameof(fileName));\n            }\n\n            switch (Path.GetExtension(fileName)?.ToLowerInvariant())\n            {\n                case \".jpg\":\n                case \".jpeg\":\n                case \".bmp\":\n                case \".png\":\n                    return;\n                default:\n                    throw new ArgumentException(\n                        \"商品券图片仅支持 JPG、JPEG、BMP 或 PNG。\",\n                        nameof(fileName));\n            }\n        }\n    }\n}\n","sourceCodeStart":803,"sourceCodeEnd":828,"githubUrl":"https://github.com/JeffreySu/WeiXinMPSDK/blob/be573f6f94bdbf718dd5f6cdecb137fbc7ff651e/src/Senparc.Weixin.TenPay/Senparc.Weixin.TenPayV3/Apis/ProductCoupon/ProductCouponApis.cs#L803-L828","documentation":"ProductCouponApis.ValidateImageFileName validates the extension of an image file name before uploading a product coupon (商品券) image to WeChat Pay. WeChat's API only accepts JPG, JPEG, BMP, or PNG images; anything else throws ArgumentException naming the offending parameter (fileName).","triggerScenarios":"Calling UploadImageAsync with a file whose extension is not .jpg/.jpeg/.bmp/.png (e.g. .gif, .webp, .GIF with no extension match, or a lowercase/uppercase mismatch the code doesn't normalize).","commonSituations":"Developers exporting marketing images as WebP or GIF from design tools, or passing URLs/paths with query strings appended to the file name so the extension check fails.","solutions":["Convert the image to PNG, JPG/JPEG, or BMP before calling UploadImageAsync.","Pass a fileName whose extension is exactly one of .jpg/.jpeg/.bmp/.png (Path.GetExtension-based check).","If the file has a query string or unusual casing, strip it or rename before upload."],"exampleFix":"// before\nawait apis.UploadImageAsyncAsync(\"banner.webp\", stream);\n// after\nvar converted = ImageToPng(\"banner.webp\");\nawait apis.UploadImageAsyncAsync(\"banner.png\", converted);","handlingStrategy":"validation","validationCode":"var ext = Path.GetExtension(fileName)?.ToLowerInvariant();\nif (ext != \".jpg\" && ext != \".jpeg\" && ext != \".bmp\" && ext != \".png\")\n    throw new ArgumentException($\"不支持的图片格式: {ext}，请转换为 JPG/BMP/PNG\", nameof(fileName));","typeGuard":null,"tryCatchPattern":"try { await apis.UploadImageAsyncAsync(fileName, stream); }\ncatch (ArgumentException ex) { logger.Warn(ex, \"商品券图片格式不支持\"); /* convert or prompt user */ }","preventionTips":["Normalize and check file extensions before upload","Convert WebP/GIF assets to PNG at ingestion time","Strip query strings from file names before passing them"],"tags":["validation","image","file-extension","argument"],"backgroundTag":"invalid-argument-value","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"}