Pages

Thursday, September 3, 2015

Magento 2: Upload the image from admin side

Here is code for upload the image from admin side in magento 2:

For directory List class

use Magento\Framework\App\Filesystem\DirectoryList;

public function __construct(

\Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory,

) {

$this->_fileUploaderFactory = $fileUploaderFactory;

}

public function uploadImage(){

$uploader = $this->_fileUploaderFactory->create(['fileId' => 'image']);

$uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);

$uploader->setAllowRenameFiles(false);

$uploader->setFilesDispersion(true);

$path = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)

->getAbsolutePath('{moduleName}/');

$result = $uploader->save($path);

}

No comments:

Post a Comment