Open Closed

User profile picture set maximum file upload limit #3648


User avatar
0
abpfintranet-1 created

Hi How can we set a file limit size for user profile picture. It seems no limit has set and I could upload a 10 MB file. I want to set like maximum 1MB, or 2Mb, ... .


1 Answer(s)
  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    Hi @abpfintranet-1, you can override the SetProfilePictureAsync method of the AccountAppService class and check whether the file size exceeds or not:

    [Dependency(ReplaceServices = true)]
    [ExposeServices(typeof(IAccountAppService), typeof(AccountAppService), typeof(MyAccountAppService))]
    public class MyAccountAppService : AccountAppService
    {
        [Authorize]
        public override async Task SetProfilePictureAsync(ProfilePictureInput input)
        {
            //check the file size and do not proceed if it's exceeded
            if(input.ImageContent.ContentLength > MaxFileSize)
            {
                return; //or throw exception
            }
             
            //file size not exceeded so, continue to set profile picture...
            
            await SettingManager.SetForUserAsync(CurrentUser.GetId(), AccountSettingNames.ProfilePictureSource, input.Type.ToString());
    
            var userIdText = CurrentUser.GetId().ToString();
    
            if (input.Type != ProfilePictureType.Image)
            {
                if (await AccountProfilePictureContainer.ExistsAsync(userIdText))
                {
                    await AccountProfilePictureContainer.DeleteAsync(userIdText);
                }
            }
            else
            {
                if (input.ImageContent == null)
                {
                    throw new NoImageProvidedException();
                }
    
                await AccountProfilePictureContainer.SaveAsync(userIdText, input.ImageContent.GetStream(), true);
            }
        }
     }
    
    
Made with ❤️ on ABP v9.1.0-preview. Updated on November 11, 2024, 11:11