Magento

Magento 2: upload file in frontend

Pinterest LinkedIn Tumblr

ADD this HTML to your .phml file which one used for file upload

<form class="upload-form" name="UploadFrom" action="<?php echo $block->getUrl('onlinewarranty/index/OnlineEmail'); ?>" id="upload-form" method="post" enctype='multipart/form-data' autocomplete="off">  
 <?= / @noEscape / $block->getBlockHtml('formkey'); ?>
<input type="file" name="upload_file" id="file-upload"/>
      <button type="submit"
              class="action submit primary "
              title="<?= $block->escapeHtml('Submit') ?>" >
              <span>
                <?= $block->escapeHtml('Submit') ?>
              </span>
      </button>
</form>

now user select the file and upload it here and when the user submits the button click the ACTION PATH controller call.
put this code into your the controller file

......................................
use Magento\Framework\App\Filesystem\DirectoryList;
......................................

class OnlineEmail extends \Magento\Framework\App\Action\Action
{
    ..........................
    protected $uploaderFactory;
    protected $adapterFactory;
    protected $filesystem;
    ..........................

    public function __construct(
        ..........................
        \Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory,
        \Magento\Framework\Image\AdapterFactory $adapterFactory,
        \Magento\Framework\Filesystem $filesystem,
        ..........................
    ) {
        ..........................
        $this->uploaderFactory = $uploaderFactory;
        $this->adapterFactory = $adapterFactory;
        $this->filesystem = $filesystem;
        ..........................
    }
    public function execute()
    {
        ......................................................................................
        ......................................................................................
        $fileup = $this->getRequest()->getFiles('upload_file');

        try {
             $uploaderFactory = $this->uploaderFactory->create(['fileId' => 'pdf']); 
                $uploaderFactory->setAllowedExtensions(['docx', 'doc', 'jpg', 'pdf']); // you can add more extension which need
                $fileAdapter = $this->adapterFactory->create();
                $uploaderFactory->setAllowRenameFiles(true);
                $uploaderFactory->setFilesDispersion(true);
                $mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
                $destinationPath = $mediaDirectory->getAbsolutePath('techspan/online');
                $result = $uploaderFactory->save($destinationPath);
                print_r($result);
                if (!$result) {
                    throw new LocalizedException(
                        __('File cannot be saved to path: $1', $destinationPath)
                    );
                }
           
            // save file name 
            $File_upoad = $result['file'];
        }  catch (\Exception $e) {
            $this->messageManager->addError(__('File not Uplaoded, Please try Agrain'));
        }
        ......................................................................................
        ......................................................................................
    }

}

Write A Comment