{"record":{"id":"e569845ffc643551","repo":"nopSolutions/nopCommerce","slug":"account-avatar-maximumuploadedfilesize","errorCode":null,"errorMessage":"Account.Avatar.MaximumUploadedFileSize","messagePattern":"Account\\.Avatar\\.MaximumUploadedFileSize","errorType":"exception","errorClass":"NopException","httpStatus":null,"severity":"warning","filePath":"src/Presentation/Nop.Web/Controllers/CustomerController.cs","lineNumber":2079,"sourceCode":"\r\n        if (!_customerSettings.AllowCustomersToUploadAvatars)\r\n            return RedirectToRoute(NopRouteNames.General.CUSTOMER_INFO);\r\n\r\n        var contentType = uploadedFile?.ContentType.ToLowerInvariant();\r\n\r\n        if (contentType != null && !contentType.Equals(\"image/jpeg\") && !contentType.Equals(\"image/gif\"))\r\n            ModelState.AddModelError(\"\", await _localizationService.GetResourceAsync(\"Account.Avatar.UploadRules\"));\r\n\r\n        if (ModelState.IsValid)\r\n        {\r\n            try\r\n            {\r\n                var customerAvatar = await _pictureService.GetPictureByIdAsync(await _genericAttributeService.GetAttributeAsync<int>(customer, NopCustomerDefaults.AvatarPictureIdAttribute));\r\n                if (uploadedFile != null && !string.IsNullOrEmpty(uploadedFile.FileName))\r\n                {\r\n                    var avatarMaxSize = _customerSettings.AvatarMaximumSizeBytes;\r\n                    if (uploadedFile.Length > avatarMaxSize)\r\n                        throw new NopException(string.Format(await _localizationService.GetResourceAsync(\"Account.Avatar.MaximumUploadedFileSize\"), avatarMaxSize));\r\n\r\n                    var customerPictureBinary = await _downloadService.GetDownloadBitsAsync(uploadedFile);\r\n                    if (customerAvatar != null)\r\n                        customerAvatar = await _pictureService.UpdatePictureAsync(customerAvatar.Id, customerPictureBinary, contentType, null);\r\n                    else\r\n                        customerAvatar = await _pictureService.InsertPictureAsync(customerPictureBinary, contentType, null);\r\n                }\r\n\r\n                var customerAvatarId = 0;\r\n                if (customerAvatar != null)\r\n                    customerAvatarId = customerAvatar.Id;\r\n\r\n                await _genericAttributeService.SaveAttributeAsync(customer, NopCustomerDefaults.AvatarPictureIdAttribute, customerAvatarId);\r\n\r\n                model.AvatarUrl = await _pictureService.GetPictureUrlAsync(\r\n                    await _genericAttributeService.GetAttributeAsync<int>(customer, NopCustomerDefaults.AvatarPictureIdAttribute),\r\n                    _mediaSettings.AvatarPictureSize,\r\n                    false);\r","sourceCodeStart":2061,"sourceCodeEnd":2097,"githubUrl":"https://github.com/nopSolutions/nopCommerce/blob/64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2/src/Presentation/Nop.Web/Controllers/CustomerController.cs#L2061-L2097","documentation":"Thrown during customer avatar upload when the uploaded file's byte length exceeds _customerSettings.AvatarMaximumSizeBytes. The message is the localized 'Account.Avatar.MaximumUploadedFileSize' resource formatted with the configured max size.","triggerScenarios":"Customer uploads an avatar image whose ContentLength/Length is greater than AvatarMaximumSizeBytes (default 20000 bytes / ~20KB) via the avatar upload action in CustomerController (line ~2079).","commonSituations":"User uploaded a high-resolution photo straight from a phone/camera (multi-MB); AvatarMaximumSizeBytes left at the very low default; the prior contentType/extension validation passed but the size did not.","solutions":["Increase AvatarMaximumSizeBytes in Customer settings (Admin > Configuration > Settings > Customer settings > Avatar) to a practical limit (e.g. 200KB+).","Instruct users to downscale/compress images before upload; add client-side size validation with a max-size check on the file input.","Server-side, optionally resize the image automatically instead of rejecting it."],"exampleFix":"// before\nif (uploadedFile.Length > avatarMaxSize)\n    throw new NopException(string.Format(await _localizationService.GetResourceAsync(\"Account.Avatar.MaximumUploadedFileSize\"), avatarMaxSize));\n\n// after: add client-side pre-check\n// <input type=\"file\" accept=\"image/*\" onchange=\"if(this.files[0].size > @avatarMaxSize){ alert('Too large'); this.value=''; }\" />","handlingStrategy":"validation","validationCode":"// Server-side pre-check before reading the stream\nvar avatarMaxSize = _customerSettings.AvatarMaximumSizeBytes;\nif (uploadedFile != null && uploadedFile.Length > avatarMaxSize)\n{\n    ModelState.AddModelError(\"\", $\"Max avatar size is {avatarMaxSize} bytes.\");\n    return View(model);\n}","typeGuard":null,"tryCatchPattern":"catch (NopException exc) when (exc.Message.Contains(\"MaximumUploadedFileSize\"))\n{\n    ModelState.AddModelError(\"\", exc.Message);\n    return View(model);\n}","preventionTips":["Add a client-side file-input size check (onchange) so users are warned before upload.","Set AvatarMaximumSizeBytes to a realistic value for modern photos.","Recommend users compress/resize images; consider server-side image resizing as an alternative."],"tags":["nopcommerce","customer","avatar","upload","file-size","configuration"],"backgroundTag":null,"analyzedSha":"64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2","analyzedAt":"2026-08-13T21:19:38.062Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}