Magento 2.3 : Multiple image upload in admin form ui component?
I am facing issue uploading multiple images.i have implemented single image upload in Magento 2.3 from the admin side using the UI component.
Thanks for your help
my code is given below.
form code
<field name="restorent_images">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="dataType" xsi:type="string">string</item>
<item name="source" xsi:type="string">geolocation</item>
<item name="label" xsi:type="string" translate="true">Image Of Restorent</item>
<item name="visible" xsi:type="boolean">true</item>
<item name="formElement" xsi:type="string">fileUploader</item>
<item name="previewTmpl" xsi:type="string">Magento_Catalog/image-preview</item>
<item name="elementTmpl" xsi:type="string">ui/form/element/uploader/uploader</item>
<!-- <item name="isMultipleFiles" xsi:type="boolean">true</item> -->
<item name="dataScope" xsi:type="string">image</item>
<item name="uploaderConfig" xsi:type="array">
<item name="url" xsi:type="url" path="ranosys_giolocation/index/upload"/>
</item>
</item>
</argument>
</field>
controller
<?php
namespace RanosysGioLocationControllerAdminhtmlIndex;
use MagentoFrameworkControllerResultFactory;
/**
* Class Upload
*/
class Upload extends MagentoBackendAppAction
{
/**
* Image uploader
*
* @var MagentoCatalogModelImageUploader
*/
protected $imageUploader;
/**
* Upload constructor.
*
* @param MagentoBackendAppActionContext $context
* @param MagentoCatalogModelImageUploader $imageUploader
*/
public function __construct(
MagentoBackendAppActionContext $context,
RanosysGioLocationModelImageUploader $imageUploader
) {
parent::__construct($context);
$this->imageUploader = $imageUploader;
}
/**
* Check admin permissions for this controller
*
* @return boolean
*/
protected function _isAllowed()
{
return $this->_authorization->isAllowed('Ranosys_GioLocation::ranosys_gioLocation');
}
/**
* Upload file controller action
*
* @return MagentoFrameworkControllerResultInterface
*/
public function execute()
{
try {
$result = $this->imageUploader->saveFileToTmpDir('restorent_images');
} catch (Exception $e) {
$result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
}
return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($result);
}
}
model
<?php
namespace RanosysGioLocationModel;
/**
* Catalog image uploader
*/
class ImageUploader
{
/**
* Core file storage database
*
* @var MagentoMediaStorageHelperFileStorageDatabase
*/
protected $coreFileStorageDatabase;
/**
* Media directory object (writable).
*
* @var MagentoFrameworkFilesystemDirectoryWriteInterface
*/
protected $mediaDirectory;
/**
* Uploader factory
*
* @var MagentoMediaStorageModelFileUploaderFactory
*/
private $uploaderFactory;
/**
* Store manager
*
* @var MagentoStoreModelStoreManagerInterface
*/
protected $storeManager;
/**
* @var PsrLogLoggerInterface
*/
protected $logger;
/**
* Base tmp path
*
* @var string
*/
protected $baseTmpPath;
/**
* Base path
*
* @var string
*/
protected $basePath;
/**
* Allowed extensions
*
* @var string
*/
protected $allowedExtensions;
public function __construct(
MagentoMediaStorageHelperFileStorageDatabase $coreFileStorageDatabase,
MagentoFrameworkFilesystem $filesystem,
MagentoMediaStorageModelFileUploaderFactory $uploaderFactory,
MagentoStoreModelStoreManagerInterface $storeManager,
PsrLogLoggerInterface $logger,
$baseTmpPath,
$basePath,
$allowedExtensions
) {
$this->coreFileStorageDatabase = $coreFileStorageDatabase;
$this->mediaDirectory = $filesystem->getDirectoryWrite(MagentoFrameworkAppFilesystemDirectoryList::MEDIA);
$this->uploaderFactory = $uploaderFactory;
$this->storeManager = $storeManager;
$this->logger = $logger;
$this->baseTmpPath = $baseTmpPath;
$this->basePath = $basePath;
$this->allowedExtensions = $allowedExtensions;
}
/**
* Set base tmp path
*
* @param string $baseTmpPath
*
* @return void
*/
public function setBaseTmpPath($baseTmpPath)
{
$this->baseTmpPath = $baseTmpPath;
}
/**
* Set base path
*
* @param string $basePath
*
* @return void
*/
public function setBasePath($basePath)
{
$this->basePath = $basePath;
}
/**
* Set allowed extensions
*
* @param string $allowedExtensions
*
* @return void
*/
public function setAllowedExtensions($allowedExtensions)
{
$this->allowedExtensions = $allowedExtensions;
}
/**
* Retrieve base tmp path
*
* @return string
*/
public function getBaseTmpPath()
{
return $this->baseTmpPath;
}
/**
* Retrieve base path
*
* @return string
*/
public function getBasePath()
{
return $this->basePath;
}
/**
* Retrieve base path
*
* @return string
*/
public function getAllowedExtensions()
{
return $this->allowedExtensions;
}
/**
* Retrieve path
*
* @param string $path
* @param string $imageName
*
* @return string
*/
public function getFilePath($path, $imageName)
{
return rtrim($path, '/') . '/' . ltrim($imageName, '/');
}
/**
* Checking file for moving and move it
*
* @param string $imageName
*
* @return string
*
* @throws MagentoFrameworkExceptionLocalizedException
*/
public function moveFileFromTmp($imageName)
{
$baseTmpPath = $this->getBaseTmpPath();
$basePath = $this->getBasePath();
$baseImagePath = $this->getFilePath($basePath, $imageName);
$baseTmpImagePath = $this->getFilePath($baseTmpPath, $imageName);
try {
$this->coreFileStorageDatabase->copyFile(
$baseTmpImagePath,
$baseImagePath
);
$this->mediaDirectory->renameFile(
$baseTmpImagePath,
$baseImagePath
);
} catch (Exception $e) {
throw new MagentoFrameworkExceptionLocalizedException(
__('Something went wrong while saving the file(s).')
);
}
return $imageName;
}
public function saveFileToTmpDir($fileId)
{
$baseTmpPath = $this->getBaseTmpPath();
$uploader = $this->uploaderFactory->create(['fileId' => $fileId]);
$uploader->setAllowedExtensions($this->getAllowedExtensions());
$uploader->setAllowRenameFiles(true);
$result = $uploader->save($this->mediaDirectory->getAbsolutePath($baseTmpPath));
if (!$result) {
throw new MagentoFrameworkExceptionLocalizedException(
__('File can not be saved to the destination folder.')
);
}
$result['tmp_name'] =str_replace(" ", "/", $result['tmp_name']);
$result['path'] = str_replace(' ', '/', $result['path']);
$result['url'] = $this->storeManager
->getStore()
->getBaseUrl(
MagentoFrameworkUrlInterface::URL_TYPE_MEDIA
) . $this->getFilePath($baseTmpPath, $result['file']);
$result['name'] = $result['file'];
if (isset($result['file'])) {
try {
$relativePath = rtrim($baseTmpPath, '/') . '/' . ltrim($result['file'], '/');
$this->coreFileStorageDatabase->saveFile($relativePath);
} catch (Exception $e) {
$this->logger->critical($e);
throw new MagentoFrameworkExceptionLocalizedException(
__('Something went wrong while saving the file(s).')
);
}
}
return $result;
}
}
magento2 uicomponent image-upload
add a comment |
I am facing issue uploading multiple images.i have implemented single image upload in Magento 2.3 from the admin side using the UI component.
Thanks for your help
my code is given below.
form code
<field name="restorent_images">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="dataType" xsi:type="string">string</item>
<item name="source" xsi:type="string">geolocation</item>
<item name="label" xsi:type="string" translate="true">Image Of Restorent</item>
<item name="visible" xsi:type="boolean">true</item>
<item name="formElement" xsi:type="string">fileUploader</item>
<item name="previewTmpl" xsi:type="string">Magento_Catalog/image-preview</item>
<item name="elementTmpl" xsi:type="string">ui/form/element/uploader/uploader</item>
<!-- <item name="isMultipleFiles" xsi:type="boolean">true</item> -->
<item name="dataScope" xsi:type="string">image</item>
<item name="uploaderConfig" xsi:type="array">
<item name="url" xsi:type="url" path="ranosys_giolocation/index/upload"/>
</item>
</item>
</argument>
</field>
controller
<?php
namespace RanosysGioLocationControllerAdminhtmlIndex;
use MagentoFrameworkControllerResultFactory;
/**
* Class Upload
*/
class Upload extends MagentoBackendAppAction
{
/**
* Image uploader
*
* @var MagentoCatalogModelImageUploader
*/
protected $imageUploader;
/**
* Upload constructor.
*
* @param MagentoBackendAppActionContext $context
* @param MagentoCatalogModelImageUploader $imageUploader
*/
public function __construct(
MagentoBackendAppActionContext $context,
RanosysGioLocationModelImageUploader $imageUploader
) {
parent::__construct($context);
$this->imageUploader = $imageUploader;
}
/**
* Check admin permissions for this controller
*
* @return boolean
*/
protected function _isAllowed()
{
return $this->_authorization->isAllowed('Ranosys_GioLocation::ranosys_gioLocation');
}
/**
* Upload file controller action
*
* @return MagentoFrameworkControllerResultInterface
*/
public function execute()
{
try {
$result = $this->imageUploader->saveFileToTmpDir('restorent_images');
} catch (Exception $e) {
$result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
}
return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($result);
}
}
model
<?php
namespace RanosysGioLocationModel;
/**
* Catalog image uploader
*/
class ImageUploader
{
/**
* Core file storage database
*
* @var MagentoMediaStorageHelperFileStorageDatabase
*/
protected $coreFileStorageDatabase;
/**
* Media directory object (writable).
*
* @var MagentoFrameworkFilesystemDirectoryWriteInterface
*/
protected $mediaDirectory;
/**
* Uploader factory
*
* @var MagentoMediaStorageModelFileUploaderFactory
*/
private $uploaderFactory;
/**
* Store manager
*
* @var MagentoStoreModelStoreManagerInterface
*/
protected $storeManager;
/**
* @var PsrLogLoggerInterface
*/
protected $logger;
/**
* Base tmp path
*
* @var string
*/
protected $baseTmpPath;
/**
* Base path
*
* @var string
*/
protected $basePath;
/**
* Allowed extensions
*
* @var string
*/
protected $allowedExtensions;
public function __construct(
MagentoMediaStorageHelperFileStorageDatabase $coreFileStorageDatabase,
MagentoFrameworkFilesystem $filesystem,
MagentoMediaStorageModelFileUploaderFactory $uploaderFactory,
MagentoStoreModelStoreManagerInterface $storeManager,
PsrLogLoggerInterface $logger,
$baseTmpPath,
$basePath,
$allowedExtensions
) {
$this->coreFileStorageDatabase = $coreFileStorageDatabase;
$this->mediaDirectory = $filesystem->getDirectoryWrite(MagentoFrameworkAppFilesystemDirectoryList::MEDIA);
$this->uploaderFactory = $uploaderFactory;
$this->storeManager = $storeManager;
$this->logger = $logger;
$this->baseTmpPath = $baseTmpPath;
$this->basePath = $basePath;
$this->allowedExtensions = $allowedExtensions;
}
/**
* Set base tmp path
*
* @param string $baseTmpPath
*
* @return void
*/
public function setBaseTmpPath($baseTmpPath)
{
$this->baseTmpPath = $baseTmpPath;
}
/**
* Set base path
*
* @param string $basePath
*
* @return void
*/
public function setBasePath($basePath)
{
$this->basePath = $basePath;
}
/**
* Set allowed extensions
*
* @param string $allowedExtensions
*
* @return void
*/
public function setAllowedExtensions($allowedExtensions)
{
$this->allowedExtensions = $allowedExtensions;
}
/**
* Retrieve base tmp path
*
* @return string
*/
public function getBaseTmpPath()
{
return $this->baseTmpPath;
}
/**
* Retrieve base path
*
* @return string
*/
public function getBasePath()
{
return $this->basePath;
}
/**
* Retrieve base path
*
* @return string
*/
public function getAllowedExtensions()
{
return $this->allowedExtensions;
}
/**
* Retrieve path
*
* @param string $path
* @param string $imageName
*
* @return string
*/
public function getFilePath($path, $imageName)
{
return rtrim($path, '/') . '/' . ltrim($imageName, '/');
}
/**
* Checking file for moving and move it
*
* @param string $imageName
*
* @return string
*
* @throws MagentoFrameworkExceptionLocalizedException
*/
public function moveFileFromTmp($imageName)
{
$baseTmpPath = $this->getBaseTmpPath();
$basePath = $this->getBasePath();
$baseImagePath = $this->getFilePath($basePath, $imageName);
$baseTmpImagePath = $this->getFilePath($baseTmpPath, $imageName);
try {
$this->coreFileStorageDatabase->copyFile(
$baseTmpImagePath,
$baseImagePath
);
$this->mediaDirectory->renameFile(
$baseTmpImagePath,
$baseImagePath
);
} catch (Exception $e) {
throw new MagentoFrameworkExceptionLocalizedException(
__('Something went wrong while saving the file(s).')
);
}
return $imageName;
}
public function saveFileToTmpDir($fileId)
{
$baseTmpPath = $this->getBaseTmpPath();
$uploader = $this->uploaderFactory->create(['fileId' => $fileId]);
$uploader->setAllowedExtensions($this->getAllowedExtensions());
$uploader->setAllowRenameFiles(true);
$result = $uploader->save($this->mediaDirectory->getAbsolutePath($baseTmpPath));
if (!$result) {
throw new MagentoFrameworkExceptionLocalizedException(
__('File can not be saved to the destination folder.')
);
}
$result['tmp_name'] =str_replace(" ", "/", $result['tmp_name']);
$result['path'] = str_replace(' ', '/', $result['path']);
$result['url'] = $this->storeManager
->getStore()
->getBaseUrl(
MagentoFrameworkUrlInterface::URL_TYPE_MEDIA
) . $this->getFilePath($baseTmpPath, $result['file']);
$result['name'] = $result['file'];
if (isset($result['file'])) {
try {
$relativePath = rtrim($baseTmpPath, '/') . '/' . ltrim($result['file'], '/');
$this->coreFileStorageDatabase->saveFile($relativePath);
} catch (Exception $e) {
$this->logger->critical($e);
throw new MagentoFrameworkExceptionLocalizedException(
__('Something went wrong while saving the file(s).')
);
}
}
return $result;
}
}
magento2 uicomponent image-upload
can you please share your code and provide error you are getting ?
– Aasim Goriya
18 mins ago
ok I had code for single image upload but i need to do multiple i will update code
– Adarsh Shukla
7 mins ago
add a comment |
I am facing issue uploading multiple images.i have implemented single image upload in Magento 2.3 from the admin side using the UI component.
Thanks for your help
my code is given below.
form code
<field name="restorent_images">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="dataType" xsi:type="string">string</item>
<item name="source" xsi:type="string">geolocation</item>
<item name="label" xsi:type="string" translate="true">Image Of Restorent</item>
<item name="visible" xsi:type="boolean">true</item>
<item name="formElement" xsi:type="string">fileUploader</item>
<item name="previewTmpl" xsi:type="string">Magento_Catalog/image-preview</item>
<item name="elementTmpl" xsi:type="string">ui/form/element/uploader/uploader</item>
<!-- <item name="isMultipleFiles" xsi:type="boolean">true</item> -->
<item name="dataScope" xsi:type="string">image</item>
<item name="uploaderConfig" xsi:type="array">
<item name="url" xsi:type="url" path="ranosys_giolocation/index/upload"/>
</item>
</item>
</argument>
</field>
controller
<?php
namespace RanosysGioLocationControllerAdminhtmlIndex;
use MagentoFrameworkControllerResultFactory;
/**
* Class Upload
*/
class Upload extends MagentoBackendAppAction
{
/**
* Image uploader
*
* @var MagentoCatalogModelImageUploader
*/
protected $imageUploader;
/**
* Upload constructor.
*
* @param MagentoBackendAppActionContext $context
* @param MagentoCatalogModelImageUploader $imageUploader
*/
public function __construct(
MagentoBackendAppActionContext $context,
RanosysGioLocationModelImageUploader $imageUploader
) {
parent::__construct($context);
$this->imageUploader = $imageUploader;
}
/**
* Check admin permissions for this controller
*
* @return boolean
*/
protected function _isAllowed()
{
return $this->_authorization->isAllowed('Ranosys_GioLocation::ranosys_gioLocation');
}
/**
* Upload file controller action
*
* @return MagentoFrameworkControllerResultInterface
*/
public function execute()
{
try {
$result = $this->imageUploader->saveFileToTmpDir('restorent_images');
} catch (Exception $e) {
$result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
}
return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($result);
}
}
model
<?php
namespace RanosysGioLocationModel;
/**
* Catalog image uploader
*/
class ImageUploader
{
/**
* Core file storage database
*
* @var MagentoMediaStorageHelperFileStorageDatabase
*/
protected $coreFileStorageDatabase;
/**
* Media directory object (writable).
*
* @var MagentoFrameworkFilesystemDirectoryWriteInterface
*/
protected $mediaDirectory;
/**
* Uploader factory
*
* @var MagentoMediaStorageModelFileUploaderFactory
*/
private $uploaderFactory;
/**
* Store manager
*
* @var MagentoStoreModelStoreManagerInterface
*/
protected $storeManager;
/**
* @var PsrLogLoggerInterface
*/
protected $logger;
/**
* Base tmp path
*
* @var string
*/
protected $baseTmpPath;
/**
* Base path
*
* @var string
*/
protected $basePath;
/**
* Allowed extensions
*
* @var string
*/
protected $allowedExtensions;
public function __construct(
MagentoMediaStorageHelperFileStorageDatabase $coreFileStorageDatabase,
MagentoFrameworkFilesystem $filesystem,
MagentoMediaStorageModelFileUploaderFactory $uploaderFactory,
MagentoStoreModelStoreManagerInterface $storeManager,
PsrLogLoggerInterface $logger,
$baseTmpPath,
$basePath,
$allowedExtensions
) {
$this->coreFileStorageDatabase = $coreFileStorageDatabase;
$this->mediaDirectory = $filesystem->getDirectoryWrite(MagentoFrameworkAppFilesystemDirectoryList::MEDIA);
$this->uploaderFactory = $uploaderFactory;
$this->storeManager = $storeManager;
$this->logger = $logger;
$this->baseTmpPath = $baseTmpPath;
$this->basePath = $basePath;
$this->allowedExtensions = $allowedExtensions;
}
/**
* Set base tmp path
*
* @param string $baseTmpPath
*
* @return void
*/
public function setBaseTmpPath($baseTmpPath)
{
$this->baseTmpPath = $baseTmpPath;
}
/**
* Set base path
*
* @param string $basePath
*
* @return void
*/
public function setBasePath($basePath)
{
$this->basePath = $basePath;
}
/**
* Set allowed extensions
*
* @param string $allowedExtensions
*
* @return void
*/
public function setAllowedExtensions($allowedExtensions)
{
$this->allowedExtensions = $allowedExtensions;
}
/**
* Retrieve base tmp path
*
* @return string
*/
public function getBaseTmpPath()
{
return $this->baseTmpPath;
}
/**
* Retrieve base path
*
* @return string
*/
public function getBasePath()
{
return $this->basePath;
}
/**
* Retrieve base path
*
* @return string
*/
public function getAllowedExtensions()
{
return $this->allowedExtensions;
}
/**
* Retrieve path
*
* @param string $path
* @param string $imageName
*
* @return string
*/
public function getFilePath($path, $imageName)
{
return rtrim($path, '/') . '/' . ltrim($imageName, '/');
}
/**
* Checking file for moving and move it
*
* @param string $imageName
*
* @return string
*
* @throws MagentoFrameworkExceptionLocalizedException
*/
public function moveFileFromTmp($imageName)
{
$baseTmpPath = $this->getBaseTmpPath();
$basePath = $this->getBasePath();
$baseImagePath = $this->getFilePath($basePath, $imageName);
$baseTmpImagePath = $this->getFilePath($baseTmpPath, $imageName);
try {
$this->coreFileStorageDatabase->copyFile(
$baseTmpImagePath,
$baseImagePath
);
$this->mediaDirectory->renameFile(
$baseTmpImagePath,
$baseImagePath
);
} catch (Exception $e) {
throw new MagentoFrameworkExceptionLocalizedException(
__('Something went wrong while saving the file(s).')
);
}
return $imageName;
}
public function saveFileToTmpDir($fileId)
{
$baseTmpPath = $this->getBaseTmpPath();
$uploader = $this->uploaderFactory->create(['fileId' => $fileId]);
$uploader->setAllowedExtensions($this->getAllowedExtensions());
$uploader->setAllowRenameFiles(true);
$result = $uploader->save($this->mediaDirectory->getAbsolutePath($baseTmpPath));
if (!$result) {
throw new MagentoFrameworkExceptionLocalizedException(
__('File can not be saved to the destination folder.')
);
}
$result['tmp_name'] =str_replace(" ", "/", $result['tmp_name']);
$result['path'] = str_replace(' ', '/', $result['path']);
$result['url'] = $this->storeManager
->getStore()
->getBaseUrl(
MagentoFrameworkUrlInterface::URL_TYPE_MEDIA
) . $this->getFilePath($baseTmpPath, $result['file']);
$result['name'] = $result['file'];
if (isset($result['file'])) {
try {
$relativePath = rtrim($baseTmpPath, '/') . '/' . ltrim($result['file'], '/');
$this->coreFileStorageDatabase->saveFile($relativePath);
} catch (Exception $e) {
$this->logger->critical($e);
throw new MagentoFrameworkExceptionLocalizedException(
__('Something went wrong while saving the file(s).')
);
}
}
return $result;
}
}
magento2 uicomponent image-upload
I am facing issue uploading multiple images.i have implemented single image upload in Magento 2.3 from the admin side using the UI component.
Thanks for your help
my code is given below.
form code
<field name="restorent_images">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="dataType" xsi:type="string">string</item>
<item name="source" xsi:type="string">geolocation</item>
<item name="label" xsi:type="string" translate="true">Image Of Restorent</item>
<item name="visible" xsi:type="boolean">true</item>
<item name="formElement" xsi:type="string">fileUploader</item>
<item name="previewTmpl" xsi:type="string">Magento_Catalog/image-preview</item>
<item name="elementTmpl" xsi:type="string">ui/form/element/uploader/uploader</item>
<!-- <item name="isMultipleFiles" xsi:type="boolean">true</item> -->
<item name="dataScope" xsi:type="string">image</item>
<item name="uploaderConfig" xsi:type="array">
<item name="url" xsi:type="url" path="ranosys_giolocation/index/upload"/>
</item>
</item>
</argument>
</field>
controller
<?php
namespace RanosysGioLocationControllerAdminhtmlIndex;
use MagentoFrameworkControllerResultFactory;
/**
* Class Upload
*/
class Upload extends MagentoBackendAppAction
{
/**
* Image uploader
*
* @var MagentoCatalogModelImageUploader
*/
protected $imageUploader;
/**
* Upload constructor.
*
* @param MagentoBackendAppActionContext $context
* @param MagentoCatalogModelImageUploader $imageUploader
*/
public function __construct(
MagentoBackendAppActionContext $context,
RanosysGioLocationModelImageUploader $imageUploader
) {
parent::__construct($context);
$this->imageUploader = $imageUploader;
}
/**
* Check admin permissions for this controller
*
* @return boolean
*/
protected function _isAllowed()
{
return $this->_authorization->isAllowed('Ranosys_GioLocation::ranosys_gioLocation');
}
/**
* Upload file controller action
*
* @return MagentoFrameworkControllerResultInterface
*/
public function execute()
{
try {
$result = $this->imageUploader->saveFileToTmpDir('restorent_images');
} catch (Exception $e) {
$result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
}
return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($result);
}
}
model
<?php
namespace RanosysGioLocationModel;
/**
* Catalog image uploader
*/
class ImageUploader
{
/**
* Core file storage database
*
* @var MagentoMediaStorageHelperFileStorageDatabase
*/
protected $coreFileStorageDatabase;
/**
* Media directory object (writable).
*
* @var MagentoFrameworkFilesystemDirectoryWriteInterface
*/
protected $mediaDirectory;
/**
* Uploader factory
*
* @var MagentoMediaStorageModelFileUploaderFactory
*/
private $uploaderFactory;
/**
* Store manager
*
* @var MagentoStoreModelStoreManagerInterface
*/
protected $storeManager;
/**
* @var PsrLogLoggerInterface
*/
protected $logger;
/**
* Base tmp path
*
* @var string
*/
protected $baseTmpPath;
/**
* Base path
*
* @var string
*/
protected $basePath;
/**
* Allowed extensions
*
* @var string
*/
protected $allowedExtensions;
public function __construct(
MagentoMediaStorageHelperFileStorageDatabase $coreFileStorageDatabase,
MagentoFrameworkFilesystem $filesystem,
MagentoMediaStorageModelFileUploaderFactory $uploaderFactory,
MagentoStoreModelStoreManagerInterface $storeManager,
PsrLogLoggerInterface $logger,
$baseTmpPath,
$basePath,
$allowedExtensions
) {
$this->coreFileStorageDatabase = $coreFileStorageDatabase;
$this->mediaDirectory = $filesystem->getDirectoryWrite(MagentoFrameworkAppFilesystemDirectoryList::MEDIA);
$this->uploaderFactory = $uploaderFactory;
$this->storeManager = $storeManager;
$this->logger = $logger;
$this->baseTmpPath = $baseTmpPath;
$this->basePath = $basePath;
$this->allowedExtensions = $allowedExtensions;
}
/**
* Set base tmp path
*
* @param string $baseTmpPath
*
* @return void
*/
public function setBaseTmpPath($baseTmpPath)
{
$this->baseTmpPath = $baseTmpPath;
}
/**
* Set base path
*
* @param string $basePath
*
* @return void
*/
public function setBasePath($basePath)
{
$this->basePath = $basePath;
}
/**
* Set allowed extensions
*
* @param string $allowedExtensions
*
* @return void
*/
public function setAllowedExtensions($allowedExtensions)
{
$this->allowedExtensions = $allowedExtensions;
}
/**
* Retrieve base tmp path
*
* @return string
*/
public function getBaseTmpPath()
{
return $this->baseTmpPath;
}
/**
* Retrieve base path
*
* @return string
*/
public function getBasePath()
{
return $this->basePath;
}
/**
* Retrieve base path
*
* @return string
*/
public function getAllowedExtensions()
{
return $this->allowedExtensions;
}
/**
* Retrieve path
*
* @param string $path
* @param string $imageName
*
* @return string
*/
public function getFilePath($path, $imageName)
{
return rtrim($path, '/') . '/' . ltrim($imageName, '/');
}
/**
* Checking file for moving and move it
*
* @param string $imageName
*
* @return string
*
* @throws MagentoFrameworkExceptionLocalizedException
*/
public function moveFileFromTmp($imageName)
{
$baseTmpPath = $this->getBaseTmpPath();
$basePath = $this->getBasePath();
$baseImagePath = $this->getFilePath($basePath, $imageName);
$baseTmpImagePath = $this->getFilePath($baseTmpPath, $imageName);
try {
$this->coreFileStorageDatabase->copyFile(
$baseTmpImagePath,
$baseImagePath
);
$this->mediaDirectory->renameFile(
$baseTmpImagePath,
$baseImagePath
);
} catch (Exception $e) {
throw new MagentoFrameworkExceptionLocalizedException(
__('Something went wrong while saving the file(s).')
);
}
return $imageName;
}
public function saveFileToTmpDir($fileId)
{
$baseTmpPath = $this->getBaseTmpPath();
$uploader = $this->uploaderFactory->create(['fileId' => $fileId]);
$uploader->setAllowedExtensions($this->getAllowedExtensions());
$uploader->setAllowRenameFiles(true);
$result = $uploader->save($this->mediaDirectory->getAbsolutePath($baseTmpPath));
if (!$result) {
throw new MagentoFrameworkExceptionLocalizedException(
__('File can not be saved to the destination folder.')
);
}
$result['tmp_name'] =str_replace(" ", "/", $result['tmp_name']);
$result['path'] = str_replace(' ', '/', $result['path']);
$result['url'] = $this->storeManager
->getStore()
->getBaseUrl(
MagentoFrameworkUrlInterface::URL_TYPE_MEDIA
) . $this->getFilePath($baseTmpPath, $result['file']);
$result['name'] = $result['file'];
if (isset($result['file'])) {
try {
$relativePath = rtrim($baseTmpPath, '/') . '/' . ltrim($result['file'], '/');
$this->coreFileStorageDatabase->saveFile($relativePath);
} catch (Exception $e) {
$this->logger->critical($e);
throw new MagentoFrameworkExceptionLocalizedException(
__('Something went wrong while saving the file(s).')
);
}
}
return $result;
}
}
magento2 uicomponent image-upload
magento2 uicomponent image-upload
edited 4 mins ago
Adarsh Shukla
asked 30 mins ago
Adarsh ShuklaAdarsh Shukla
12213
12213
can you please share your code and provide error you are getting ?
– Aasim Goriya
18 mins ago
ok I had code for single image upload but i need to do multiple i will update code
– Adarsh Shukla
7 mins ago
add a comment |
can you please share your code and provide error you are getting ?
– Aasim Goriya
18 mins ago
ok I had code for single image upload but i need to do multiple i will update code
– Adarsh Shukla
7 mins ago
can you please share your code and provide error you are getting ?
– Aasim Goriya
18 mins ago
can you please share your code and provide error you are getting ?
– Aasim Goriya
18 mins ago
ok I had code for single image upload but i need to do multiple i will update code
– Adarsh Shukla
7 mins ago
ok I had code for single image upload but i need to do multiple i will update code
– Adarsh Shukla
7 mins ago
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f261322%2fmagento-2-3-multiple-image-upload-in-admin-form-ui-component%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f261322%2fmagento-2-3-multiple-image-upload-in-admin-form-ui-component%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
can you please share your code and provide error you are getting ?
– Aasim Goriya
18 mins ago
ok I had code for single image upload but i need to do multiple i will update code
– Adarsh Shukla
7 mins ago