1

I have created a custom file type customer attribute

$customerSetup -> addAttribute(\Magento\Customer\Model\Customer::ENTITY,
    'price_agreement',
    [
        'type' => 'text',
        'label' => 'Price Agreement File',
        'input' => 'file',
        'source' => '',
        'required' => false,
        'visible' => true,
        'position' => 200,
        'system' => false,
        'backend' => ''
    ]
    );

$customerSetup->getEavConfig()->getAttribute('customer', 'price_agreement')->setData('is_user_defined',1)
                                 ->setData('is_required',0)
                                 ->setData('default_value','')
                                 ->setData('used_in_forms', ['adminhtml_customer', 'customer_account_create', 'customer_account_edit']) 
                                 ->save();                                 

}

I want to restrict this field to upload only .pdf format. No other format should not be able to upload.Where can I set setAllowedExtensions for this attribute.

Any help??

2
  • Try to check the below process of adding a product attribute of file type. It may help you.. webkul.com/blog/… – nishu Jun 17 '19 at 8:21
  • Did you find any solution for this if so can you please post here. – Mage2 Beginner Oct 9 '20 at 16:06
1

Please try with below code: step1 :

<input type="file" name="custom_file_name" id="custom_file_name" title="PDF File" class="input-text" data-validate="{required:true}">

step2 :

<?php
namespace Adamsmage\Fileupload\Controller;

use Magento\Framework\App\Action\Context;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\Filesystem;
use Magento\MediaStorage\Model\File\UploaderFactory;

class Fileupload extends \Magento\Framework\App\Action\Action
{
    /**
     * @var \Magento\Framework\Message\ManagerInterface
     */
    protected $messageManager;

    /**
     * @var \Magento\Framework\Filesystem $filesystem
     */
    protected $filesystem;

    /**
     * @var \Magento\MediaStorage\Model\File\UploaderFactory $fileUploader
     */
    protected $fileUploader;

    public function __construct(
        Context $context,
        ManagerInterface $messageManager,
        ...
        Filesystem $filesystem,
        UploaderFactory $fileUploader
    )
    {
        $this->messageManager       = $messageManager;
        ...
        $this->filesystem           = $filesystem;
        $this->fileUploader         = $fileUploader;

        $this->mediaDirectory = $filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);

        parent::__construct($context);
    }

    public function execute()
    {
        $uploadedFile = $this->uploadpdfFile();
    }

    public function uploadpdfFile()
    {
        // this folder will be created inside "pub/media" folder
        $yourFolderName = 'custom-folder-name/';

        // "my_custom_file" is the HTML input file name
        $yourInputFileName = 'custom_file_name';

        try{
            $file = $this->getRequest()->getFiles($yourInputFileName);
            $fileName = ($file && array_key_exists('name', $file)) ? $file['name'] : null;

            if ($file && $fileName) {
                $target = $this->mediaDirectory->getAbsolutePath($yourFolderName);        

                /** @var $uploader \Magento\MediaStorage\Model\File\Uploader */
                $uploader = $this->fileUploader->create(['fileId' => $yourInputFileName]);

                // set allowed file extensions
                $uploader->setAllowedExtensions(['pdf']);

                // allow folder creation
                $uploader->setAllowCreateFolders(true);

                // rename file name if already exists 
                $uploader->setAllowRenameFiles(true);

                // upload file in the specified folder
                $result = $uploader->save($target);

                if ($result['file']) {
                    $this->messageManager->addSuccess(__('File has been successfully uploaded.')); 
                }

                return $target . $uploader->getUploadedFileName();
            }
        } catch (\Exception $e) {
            $this->messageManager->addError($e->getMessage());
        }

        return false;
    }
}
1
  • if other file format uploaded, need to show error message, that this type is not allowed. how can we do that @Mansuri. – jafar pinjar Nov 19 '20 at 13:30
0

Adding eg accept=".csv, text/csv" in the input tag. Then only pdf can choose.

escapeHtml($fieldNameFile) ?>">

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file

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.