Trying to update custom customer attribute value while customer edit from frontend , and my field is file upload control but unable to save the attribute


$customer = $observer->getEvent()->getCustomer();
$customer->setCustomAttribute('profileimage', '123.jpg');

but getting error Call to a member function setCustomAttribute() on null

secondly, I have also try

$event = $observer->getEvent();
$customer = $observer->getCustomerDataObject();
$customerId = $customer->getId();
$customerData = $this->_customerRepositoryInterface->getById($customerId);
$customerData->setCustomAttribute('profileimage', '123.jpg');
$customerResource = $this->customerFactory->create();
$customerResource->saveAttribute($customer, 'profileimage');

Still getting error for updateData() function not found.

can anyone help me out to save custom customer attribute from frontend for image in Magento 2.

  • How you create the attribute? Share that code. – Dhiren Vasoya Jan 4 at 11:01

You can try something like below.

protected $customer;

protected $customerFactory;

public function __construct(
    \Magento\Customer\Model\Customer $customer
    \Magento\Customer\Model\ResourceModel\CustomerFactory $customerFactory
    $this->customer = $customer;
    $this->customerFactory = $customerFactory;

$customerId = "1";
$customer = $this->customer->load($customerId);
$data = "customer attribute value";
$customerData = $customer->getDataModel();
$customerResource = $this->customerFactory->create();
$customerResource->saveAttribute($customer, 'customer_attribute_code');

It will work fine. Please check and let me know still you got the error.

Also, you can download extension which is already developed.


  • Tried above code but unfortunately it doesn't work for me – Amit Hariyani Jan 5 at 4:17
  • Tried above code but unfortunately it doesn't work for me , the custom attribute which i have created is of type 'file' and whatever solutions i have got works for input type 'text' not for 'file'. my custom field is $customerSetup->addAttribute( \Magento\Customer\Model\Customer::ENTITY, 'profileimage', [ 'type' => 'text', 'label' => 'Profile Picture', 'input' => 'file', not working for this. – Amit Hariyani Jan 5 at 4:24
  • while trying to update attribute of type 'file' i'm getting below error Fatal error: Uncaught Magento\Framework\Exception\FileSystemException: Cannot gather stats! Warning!stat(): stat failed for C:/xampp741/htdocs/mag240s/pub/media/customer/123.jpg in C:\xampp741\htdocs\mag240s\vendor\magento\framework\Filesystem\Driver\File.php:77 – Amit Hariyani Jan 5 at 4:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.