How to do image uploader in the custom form field in magento 2 backend Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Magento2 : Admin module Image upload code to display formMagento2 Admin Grid Image DisplayIn admin form after edit and save a form in db, already stored image save as blank in db in magento 2Magento 2: How to override newsletter Subscriber modelMagento 2.1 : Multiple Image Uploader in Ui Component FormPrice not getting updated for second product…Magento 2 Add new field to Magento_User admin formMagento 2 - Get Swatch Image based on swatch labelDI not working in ControllerMagento 2 - Use of the Media Image UploaderForm is not displayed on panel admin Magento 2Backend Image uploader not workingHow to get image from image uploader file path and bind in custom collection? Magento 2
How does the Nova's Burn power work at the 7-9 level?
Antler Helmet: Can it work?
How can players take actions together that are impossible otherwise?
Slither Like a Snake
How to market an anarchic city as a tourism spot to people living in civilized areas?
Two different pronunciation of "понял"
Is there a documented rationale why the House Ways and Means chairman can demand tax info?
Fishing simulator
Is drag coefficient lowest at zero angle of attack?
Can I throw a longsword at someone?
Active filter with series inductor and resistor - do these exist?
Replacing HDD with SSD; what about non-APFS/APFS?
How do I keep my slimes from escaping their pens?
Is it possible to ask for a hotel room without minibar/extra services?
What are the performance impacts of 'functional' Rust?
What did Darwin mean by 'squib' here?
Determine whether or not the following series converge.
Typsetting diagram chases (with TikZ?)
What do you call the holes in a flute?
Windows 10: How to Lock (not sleep) laptop on lid close?
Single author papers against my advisor's will?
Statistical model of ligand substitution
How does modal jazz use chord progressions?
Why is there no army of Iron-Mans in the MCU?
How to do image uploader in the custom form field in magento 2 backend
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?Magento2 : Admin module Image upload code to display formMagento2 Admin Grid Image DisplayIn admin form after edit and save a form in db, already stored image save as blank in db in magento 2Magento 2: How to override newsletter Subscriber modelMagento 2.1 : Multiple Image Uploader in Ui Component FormPrice not getting updated for second product…Magento 2 Add new field to Magento_User admin formMagento 2 - Get Swatch Image based on swatch labelDI not working in ControllerMagento 2 - Use of the Media Image UploaderForm is not displayed on panel admin Magento 2Backend Image uploader not workingHow to get image from image uploader file path and bind in custom collection? Magento 2
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
How to do image uploader in the custom form field in magento 2 backen
_prepareForm()
$fieldset->addField(
'proimage',
'image',
[
'title' => __('Image'),
'label' => __(' Image'),
'name' => 'proimage',
'note' => 'Allow image type: jpg, jpeg, gif, png',
]
);
Save.php controller
<?php
namespace CmProductlabelControllerAdminhtmlLabel;
use MagentoBackendAppAction;
use MagentoFrameworkAppFilesystemDirectoryList;
class Save extends Action
/**
* @param ActionContext $context
*/
public function __construct(
ActionContext $context,
)
parent::__construct($context);
protected function _isAllowed()
return $this->_authorization->isAllowed('Cm_Productlabel::save');
public function execute()
$isPost = $this->getRequest()->getPost();
$resultRedirect = $this->resultRedirectFactory->create();
if ($isPost)
$model = $this->_objectManager-
>create('CmProductlabelModelProductlabel');
$postId = $this->getRequest()->getParam('productlabel_id');
if ($postId)
$model->load($postId);
$profileImage = $this->getRequest()->getFiles('proimage');
$fileName = ($profileImage && array_key_exists('name', $profileImage)) ?
$profileImage['name'] : null;
if ($profileImage && $fileName)
try
$uploader = $this->_objectManager->create(
'MagentoMediaStorageModelFileUploader',
['fileId' => 'proimage']
);
$uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
/** @var MagentoFrameworkImageAdapterAdapterInterface
$imageAdapterFactory */
$imageAdapterFactory = $this->_objectManager-
>get('MagentoFrameworkImageAdapterFactory')
->create();
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(true);
$uploader->setAllowCreateFolders(true);
/** @var MagentoFrameworkFilesystemDirectoryRead
$mediaDirectory */
$mediaDirectory = $this->_objectManager-
>get('MagentoFrameworkFilesystem')
->getDirectoryRead(DirectoryList::MEDIA);
$result = $uploader->save(
$mediaDirectory
->getAbsolutePath('Bootsgrid/Productlabel')
);
//$data['profile'] = 'Modulename/Profile/'. $result['file'];
$model->setProimage('Bootsgrid/Productlabel'.$result['file']); /
/Database field name
catch (Exception $e)
if ($e->getCode() == 0)
$this->messageManager->addError($e->getMessage());
$formData = $this->getRequest()->getParam('label');
$model->setData($formData);
try
// Save news
$model->save();
// Display success message
$this->messageManager->addSuccess(__('The post has been
saved.'));
// Check if 'Save and Continue'
if ($this->getRequest()->getParam('back'))
$this->_redirect('*/*/edit', ['id' => $model->getId(),
'_current' => true]);
return;
// Go to grid page
$this->_redirect('*/*/');
return;
catch (Exception $e)
$this->messageManager->addError($e->getMessage());
$this->_getSession()->setFormData($formData);
$this->_redirect('*/*/edit', ['id' => $postId]);
magento2 magento-2.1 magento2.3
add a comment |
How to do image uploader in the custom form field in magento 2 backen
_prepareForm()
$fieldset->addField(
'proimage',
'image',
[
'title' => __('Image'),
'label' => __(' Image'),
'name' => 'proimage',
'note' => 'Allow image type: jpg, jpeg, gif, png',
]
);
Save.php controller
<?php
namespace CmProductlabelControllerAdminhtmlLabel;
use MagentoBackendAppAction;
use MagentoFrameworkAppFilesystemDirectoryList;
class Save extends Action
/**
* @param ActionContext $context
*/
public function __construct(
ActionContext $context,
)
parent::__construct($context);
protected function _isAllowed()
return $this->_authorization->isAllowed('Cm_Productlabel::save');
public function execute()
$isPost = $this->getRequest()->getPost();
$resultRedirect = $this->resultRedirectFactory->create();
if ($isPost)
$model = $this->_objectManager-
>create('CmProductlabelModelProductlabel');
$postId = $this->getRequest()->getParam('productlabel_id');
if ($postId)
$model->load($postId);
$profileImage = $this->getRequest()->getFiles('proimage');
$fileName = ($profileImage && array_key_exists('name', $profileImage)) ?
$profileImage['name'] : null;
if ($profileImage && $fileName)
try
$uploader = $this->_objectManager->create(
'MagentoMediaStorageModelFileUploader',
['fileId' => 'proimage']
);
$uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
/** @var MagentoFrameworkImageAdapterAdapterInterface
$imageAdapterFactory */
$imageAdapterFactory = $this->_objectManager-
>get('MagentoFrameworkImageAdapterFactory')
->create();
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(true);
$uploader->setAllowCreateFolders(true);
/** @var MagentoFrameworkFilesystemDirectoryRead
$mediaDirectory */
$mediaDirectory = $this->_objectManager-
>get('MagentoFrameworkFilesystem')
->getDirectoryRead(DirectoryList::MEDIA);
$result = $uploader->save(
$mediaDirectory
->getAbsolutePath('Bootsgrid/Productlabel')
);
//$data['profile'] = 'Modulename/Profile/'. $result['file'];
$model->setProimage('Bootsgrid/Productlabel'.$result['file']); /
/Database field name
catch (Exception $e)
if ($e->getCode() == 0)
$this->messageManager->addError($e->getMessage());
$formData = $this->getRequest()->getParam('label');
$model->setData($formData);
try
// Save news
$model->save();
// Display success message
$this->messageManager->addSuccess(__('The post has been
saved.'));
// Check if 'Save and Continue'
if ($this->getRequest()->getParam('back'))
$this->_redirect('*/*/edit', ['id' => $model->getId(),
'_current' => true]);
return;
// Go to grid page
$this->_redirect('*/*/');
return;
catch (Exception $e)
$this->messageManager->addError($e->getMessage());
$this->_getSession()->setFormData($formData);
$this->_redirect('*/*/edit', ['id' => $postId]);
magento2 magento-2.1 magento2.3
1
Possible duplicate of Magento2 : Admin module Image upload code to display form
– Jaimin Sutariya
Apr 11 at 6:34
i used it but uploaded image not saved in database, i m looking forward into the issue @rakesh
– divya sekar
Apr 11 at 8:48
ok no problem @divyasekar
– Rakesh Donga
Apr 11 at 8:56
add a comment |
How to do image uploader in the custom form field in magento 2 backen
_prepareForm()
$fieldset->addField(
'proimage',
'image',
[
'title' => __('Image'),
'label' => __(' Image'),
'name' => 'proimage',
'note' => 'Allow image type: jpg, jpeg, gif, png',
]
);
Save.php controller
<?php
namespace CmProductlabelControllerAdminhtmlLabel;
use MagentoBackendAppAction;
use MagentoFrameworkAppFilesystemDirectoryList;
class Save extends Action
/**
* @param ActionContext $context
*/
public function __construct(
ActionContext $context,
)
parent::__construct($context);
protected function _isAllowed()
return $this->_authorization->isAllowed('Cm_Productlabel::save');
public function execute()
$isPost = $this->getRequest()->getPost();
$resultRedirect = $this->resultRedirectFactory->create();
if ($isPost)
$model = $this->_objectManager-
>create('CmProductlabelModelProductlabel');
$postId = $this->getRequest()->getParam('productlabel_id');
if ($postId)
$model->load($postId);
$profileImage = $this->getRequest()->getFiles('proimage');
$fileName = ($profileImage && array_key_exists('name', $profileImage)) ?
$profileImage['name'] : null;
if ($profileImage && $fileName)
try
$uploader = $this->_objectManager->create(
'MagentoMediaStorageModelFileUploader',
['fileId' => 'proimage']
);
$uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
/** @var MagentoFrameworkImageAdapterAdapterInterface
$imageAdapterFactory */
$imageAdapterFactory = $this->_objectManager-
>get('MagentoFrameworkImageAdapterFactory')
->create();
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(true);
$uploader->setAllowCreateFolders(true);
/** @var MagentoFrameworkFilesystemDirectoryRead
$mediaDirectory */
$mediaDirectory = $this->_objectManager-
>get('MagentoFrameworkFilesystem')
->getDirectoryRead(DirectoryList::MEDIA);
$result = $uploader->save(
$mediaDirectory
->getAbsolutePath('Bootsgrid/Productlabel')
);
//$data['profile'] = 'Modulename/Profile/'. $result['file'];
$model->setProimage('Bootsgrid/Productlabel'.$result['file']); /
/Database field name
catch (Exception $e)
if ($e->getCode() == 0)
$this->messageManager->addError($e->getMessage());
$formData = $this->getRequest()->getParam('label');
$model->setData($formData);
try
// Save news
$model->save();
// Display success message
$this->messageManager->addSuccess(__('The post has been
saved.'));
// Check if 'Save and Continue'
if ($this->getRequest()->getParam('back'))
$this->_redirect('*/*/edit', ['id' => $model->getId(),
'_current' => true]);
return;
// Go to grid page
$this->_redirect('*/*/');
return;
catch (Exception $e)
$this->messageManager->addError($e->getMessage());
$this->_getSession()->setFormData($formData);
$this->_redirect('*/*/edit', ['id' => $postId]);
magento2 magento-2.1 magento2.3
How to do image uploader in the custom form field in magento 2 backen
_prepareForm()
$fieldset->addField(
'proimage',
'image',
[
'title' => __('Image'),
'label' => __(' Image'),
'name' => 'proimage',
'note' => 'Allow image type: jpg, jpeg, gif, png',
]
);
Save.php controller
<?php
namespace CmProductlabelControllerAdminhtmlLabel;
use MagentoBackendAppAction;
use MagentoFrameworkAppFilesystemDirectoryList;
class Save extends Action
/**
* @param ActionContext $context
*/
public function __construct(
ActionContext $context,
)
parent::__construct($context);
protected function _isAllowed()
return $this->_authorization->isAllowed('Cm_Productlabel::save');
public function execute()
$isPost = $this->getRequest()->getPost();
$resultRedirect = $this->resultRedirectFactory->create();
if ($isPost)
$model = $this->_objectManager-
>create('CmProductlabelModelProductlabel');
$postId = $this->getRequest()->getParam('productlabel_id');
if ($postId)
$model->load($postId);
$profileImage = $this->getRequest()->getFiles('proimage');
$fileName = ($profileImage && array_key_exists('name', $profileImage)) ?
$profileImage['name'] : null;
if ($profileImage && $fileName)
try
$uploader = $this->_objectManager->create(
'MagentoMediaStorageModelFileUploader',
['fileId' => 'proimage']
);
$uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
/** @var MagentoFrameworkImageAdapterAdapterInterface
$imageAdapterFactory */
$imageAdapterFactory = $this->_objectManager-
>get('MagentoFrameworkImageAdapterFactory')
->create();
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(true);
$uploader->setAllowCreateFolders(true);
/** @var MagentoFrameworkFilesystemDirectoryRead
$mediaDirectory */
$mediaDirectory = $this->_objectManager-
>get('MagentoFrameworkFilesystem')
->getDirectoryRead(DirectoryList::MEDIA);
$result = $uploader->save(
$mediaDirectory
->getAbsolutePath('Bootsgrid/Productlabel')
);
//$data['profile'] = 'Modulename/Profile/'. $result['file'];
$model->setProimage('Bootsgrid/Productlabel'.$result['file']); /
/Database field name
catch (Exception $e)
if ($e->getCode() == 0)
$this->messageManager->addError($e->getMessage());
$formData = $this->getRequest()->getParam('label');
$model->setData($formData);
try
// Save news
$model->save();
// Display success message
$this->messageManager->addSuccess(__('The post has been
saved.'));
// Check if 'Save and Continue'
if ($this->getRequest()->getParam('back'))
$this->_redirect('*/*/edit', ['id' => $model->getId(),
'_current' => true]);
return;
// Go to grid page
$this->_redirect('*/*/');
return;
catch (Exception $e)
$this->messageManager->addError($e->getMessage());
$this->_getSession()->setFormData($formData);
$this->_redirect('*/*/edit', ['id' => $postId]);
magento2 magento-2.1 magento2.3
magento2 magento-2.1 magento2.3
edited Apr 11 at 12:40
Rakesh Donga
2,491316
2,491316
asked Apr 11 at 5:47
divya sekardivya sekar
32614
32614
1
Possible duplicate of Magento2 : Admin module Image upload code to display form
– Jaimin Sutariya
Apr 11 at 6:34
i used it but uploaded image not saved in database, i m looking forward into the issue @rakesh
– divya sekar
Apr 11 at 8:48
ok no problem @divyasekar
– Rakesh Donga
Apr 11 at 8:56
add a comment |
1
Possible duplicate of Magento2 : Admin module Image upload code to display form
– Jaimin Sutariya
Apr 11 at 6:34
i used it but uploaded image not saved in database, i m looking forward into the issue @rakesh
– divya sekar
Apr 11 at 8:48
ok no problem @divyasekar
– Rakesh Donga
Apr 11 at 8:56
1
1
Possible duplicate of Magento2 : Admin module Image upload code to display form
– Jaimin Sutariya
Apr 11 at 6:34
Possible duplicate of Magento2 : Admin module Image upload code to display form
– Jaimin Sutariya
Apr 11 at 6:34
i used it but uploaded image not saved in database, i m looking forward into the issue @rakesh
– divya sekar
Apr 11 at 8:48
i used it but uploaded image not saved in database, i m looking forward into the issue @rakesh
– divya sekar
Apr 11 at 8:48
ok no problem @divyasekar
– Rakesh Donga
Apr 11 at 8:56
ok no problem @divyasekar
– Rakesh Donga
Apr 11 at 8:56
add a comment |
1 Answer
1
active
oldest
votes
Add Below code in n _prepareForm()
function inside
app/code/Vendor/Module/Block/Adminhtml/Module/Edit/Tab/Main.php
$fieldset->addField(
'profile',
'image',
[
'name' => 'profile',
'label' => __('Profile Image'),
'title' => __('Profile Image'),
'required' => false,
'disabled' => $isElementDisabled
]
);
Now in execute()
method of your save controller file. use below code to save and upload image.
app/code/Vendor/Module/Controller/Adminhtml/Index/Save.php
use MagentoFrameworkAppFilesystemDirectoryList;
$profileImage = $this->getRequest()->getFiles('profile');
$fileName = ($profileImage && array_key_exists('name', $profileImage)) ? $profileImage['name'] : null;
if ($profileImage && $fileName)
try
/** @var MagentoFrameworkObjectManagerInterface $uploader */
$uploader = $this->_objectManager->create(
'MagentoMediaStorageModelFileUploader',
['fileId' => 'profile']
);
$uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
/** @var MagentoFrameworkImageAdapterAdapterInterface $imageAdapterFactory */
$imageAdapterFactory = $this->_objectManager->get('MagentoFrameworkImageAdapterFactory')
->create();
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(true);
$uploader->setAllowCreateFolders(true);
/** @var MagentoFrameworkFilesystemDirectoryRead $mediaDirectory */
$mediaDirectory = $this->_objectManager->get('MagentoFrameworkFilesystem')
->getDirectoryRead(DirectoryList::MEDIA);
$result = $uploader->save(
$mediaDirectory
->getAbsolutePath('Modulename/Profile')
);
//$data['profile'] = 'Modulename/Profile/'. $result['file'];
$model->setProfile('Modulename/Profile'.$result['file']); //Database field name
catch (Exception $e)
if ($e->getCode() == 0)
$this->messageManager->addError($e->getMessage());
And Image showing in grid follow this link : Reference
when i save a image, in save.php controller file i get a null value prnt.sc/nag0sd @rakesh
– divya sekar
Apr 11 at 9:06
i used a var_dump() function it reflects null prnt.sc/nag2ni @rakesh
– divya sekar
Apr 11 at 9:06
its not working
– divya sekar
Apr 11 at 10:46
@divyasekar it is working code i have many time used you are small mistake any where
– Rakesh Donga
Apr 11 at 10:50
wait i will post a code
– divya sekar
Apr 11 at 10:51
|
show 9 more comments
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%2f269632%2fhow-to-do-image-uploader-in-the-custom-form-field-in-magento-2-backend%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Add Below code in n _prepareForm()
function inside
app/code/Vendor/Module/Block/Adminhtml/Module/Edit/Tab/Main.php
$fieldset->addField(
'profile',
'image',
[
'name' => 'profile',
'label' => __('Profile Image'),
'title' => __('Profile Image'),
'required' => false,
'disabled' => $isElementDisabled
]
);
Now in execute()
method of your save controller file. use below code to save and upload image.
app/code/Vendor/Module/Controller/Adminhtml/Index/Save.php
use MagentoFrameworkAppFilesystemDirectoryList;
$profileImage = $this->getRequest()->getFiles('profile');
$fileName = ($profileImage && array_key_exists('name', $profileImage)) ? $profileImage['name'] : null;
if ($profileImage && $fileName)
try
/** @var MagentoFrameworkObjectManagerInterface $uploader */
$uploader = $this->_objectManager->create(
'MagentoMediaStorageModelFileUploader',
['fileId' => 'profile']
);
$uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
/** @var MagentoFrameworkImageAdapterAdapterInterface $imageAdapterFactory */
$imageAdapterFactory = $this->_objectManager->get('MagentoFrameworkImageAdapterFactory')
->create();
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(true);
$uploader->setAllowCreateFolders(true);
/** @var MagentoFrameworkFilesystemDirectoryRead $mediaDirectory */
$mediaDirectory = $this->_objectManager->get('MagentoFrameworkFilesystem')
->getDirectoryRead(DirectoryList::MEDIA);
$result = $uploader->save(
$mediaDirectory
->getAbsolutePath('Modulename/Profile')
);
//$data['profile'] = 'Modulename/Profile/'. $result['file'];
$model->setProfile('Modulename/Profile'.$result['file']); //Database field name
catch (Exception $e)
if ($e->getCode() == 0)
$this->messageManager->addError($e->getMessage());
And Image showing in grid follow this link : Reference
when i save a image, in save.php controller file i get a null value prnt.sc/nag0sd @rakesh
– divya sekar
Apr 11 at 9:06
i used a var_dump() function it reflects null prnt.sc/nag2ni @rakesh
– divya sekar
Apr 11 at 9:06
its not working
– divya sekar
Apr 11 at 10:46
@divyasekar it is working code i have many time used you are small mistake any where
– Rakesh Donga
Apr 11 at 10:50
wait i will post a code
– divya sekar
Apr 11 at 10:51
|
show 9 more comments
Add Below code in n _prepareForm()
function inside
app/code/Vendor/Module/Block/Adminhtml/Module/Edit/Tab/Main.php
$fieldset->addField(
'profile',
'image',
[
'name' => 'profile',
'label' => __('Profile Image'),
'title' => __('Profile Image'),
'required' => false,
'disabled' => $isElementDisabled
]
);
Now in execute()
method of your save controller file. use below code to save and upload image.
app/code/Vendor/Module/Controller/Adminhtml/Index/Save.php
use MagentoFrameworkAppFilesystemDirectoryList;
$profileImage = $this->getRequest()->getFiles('profile');
$fileName = ($profileImage && array_key_exists('name', $profileImage)) ? $profileImage['name'] : null;
if ($profileImage && $fileName)
try
/** @var MagentoFrameworkObjectManagerInterface $uploader */
$uploader = $this->_objectManager->create(
'MagentoMediaStorageModelFileUploader',
['fileId' => 'profile']
);
$uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
/** @var MagentoFrameworkImageAdapterAdapterInterface $imageAdapterFactory */
$imageAdapterFactory = $this->_objectManager->get('MagentoFrameworkImageAdapterFactory')
->create();
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(true);
$uploader->setAllowCreateFolders(true);
/** @var MagentoFrameworkFilesystemDirectoryRead $mediaDirectory */
$mediaDirectory = $this->_objectManager->get('MagentoFrameworkFilesystem')
->getDirectoryRead(DirectoryList::MEDIA);
$result = $uploader->save(
$mediaDirectory
->getAbsolutePath('Modulename/Profile')
);
//$data['profile'] = 'Modulename/Profile/'. $result['file'];
$model->setProfile('Modulename/Profile'.$result['file']); //Database field name
catch (Exception $e)
if ($e->getCode() == 0)
$this->messageManager->addError($e->getMessage());
And Image showing in grid follow this link : Reference
when i save a image, in save.php controller file i get a null value prnt.sc/nag0sd @rakesh
– divya sekar
Apr 11 at 9:06
i used a var_dump() function it reflects null prnt.sc/nag2ni @rakesh
– divya sekar
Apr 11 at 9:06
its not working
– divya sekar
Apr 11 at 10:46
@divyasekar it is working code i have many time used you are small mistake any where
– Rakesh Donga
Apr 11 at 10:50
wait i will post a code
– divya sekar
Apr 11 at 10:51
|
show 9 more comments
Add Below code in n _prepareForm()
function inside
app/code/Vendor/Module/Block/Adminhtml/Module/Edit/Tab/Main.php
$fieldset->addField(
'profile',
'image',
[
'name' => 'profile',
'label' => __('Profile Image'),
'title' => __('Profile Image'),
'required' => false,
'disabled' => $isElementDisabled
]
);
Now in execute()
method of your save controller file. use below code to save and upload image.
app/code/Vendor/Module/Controller/Adminhtml/Index/Save.php
use MagentoFrameworkAppFilesystemDirectoryList;
$profileImage = $this->getRequest()->getFiles('profile');
$fileName = ($profileImage && array_key_exists('name', $profileImage)) ? $profileImage['name'] : null;
if ($profileImage && $fileName)
try
/** @var MagentoFrameworkObjectManagerInterface $uploader */
$uploader = $this->_objectManager->create(
'MagentoMediaStorageModelFileUploader',
['fileId' => 'profile']
);
$uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
/** @var MagentoFrameworkImageAdapterAdapterInterface $imageAdapterFactory */
$imageAdapterFactory = $this->_objectManager->get('MagentoFrameworkImageAdapterFactory')
->create();
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(true);
$uploader->setAllowCreateFolders(true);
/** @var MagentoFrameworkFilesystemDirectoryRead $mediaDirectory */
$mediaDirectory = $this->_objectManager->get('MagentoFrameworkFilesystem')
->getDirectoryRead(DirectoryList::MEDIA);
$result = $uploader->save(
$mediaDirectory
->getAbsolutePath('Modulename/Profile')
);
//$data['profile'] = 'Modulename/Profile/'. $result['file'];
$model->setProfile('Modulename/Profile'.$result['file']); //Database field name
catch (Exception $e)
if ($e->getCode() == 0)
$this->messageManager->addError($e->getMessage());
And Image showing in grid follow this link : Reference
Add Below code in n _prepareForm()
function inside
app/code/Vendor/Module/Block/Adminhtml/Module/Edit/Tab/Main.php
$fieldset->addField(
'profile',
'image',
[
'name' => 'profile',
'label' => __('Profile Image'),
'title' => __('Profile Image'),
'required' => false,
'disabled' => $isElementDisabled
]
);
Now in execute()
method of your save controller file. use below code to save and upload image.
app/code/Vendor/Module/Controller/Adminhtml/Index/Save.php
use MagentoFrameworkAppFilesystemDirectoryList;
$profileImage = $this->getRequest()->getFiles('profile');
$fileName = ($profileImage && array_key_exists('name', $profileImage)) ? $profileImage['name'] : null;
if ($profileImage && $fileName)
try
/** @var MagentoFrameworkObjectManagerInterface $uploader */
$uploader = $this->_objectManager->create(
'MagentoMediaStorageModelFileUploader',
['fileId' => 'profile']
);
$uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
/** @var MagentoFrameworkImageAdapterAdapterInterface $imageAdapterFactory */
$imageAdapterFactory = $this->_objectManager->get('MagentoFrameworkImageAdapterFactory')
->create();
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(true);
$uploader->setAllowCreateFolders(true);
/** @var MagentoFrameworkFilesystemDirectoryRead $mediaDirectory */
$mediaDirectory = $this->_objectManager->get('MagentoFrameworkFilesystem')
->getDirectoryRead(DirectoryList::MEDIA);
$result = $uploader->save(
$mediaDirectory
->getAbsolutePath('Modulename/Profile')
);
//$data['profile'] = 'Modulename/Profile/'. $result['file'];
$model->setProfile('Modulename/Profile'.$result['file']); //Database field name
catch (Exception $e)
if ($e->getCode() == 0)
$this->messageManager->addError($e->getMessage());
And Image showing in grid follow this link : Reference
answered Apr 11 at 6:13
Rakesh DongaRakesh Donga
2,491316
2,491316
when i save a image, in save.php controller file i get a null value prnt.sc/nag0sd @rakesh
– divya sekar
Apr 11 at 9:06
i used a var_dump() function it reflects null prnt.sc/nag2ni @rakesh
– divya sekar
Apr 11 at 9:06
its not working
– divya sekar
Apr 11 at 10:46
@divyasekar it is working code i have many time used you are small mistake any where
– Rakesh Donga
Apr 11 at 10:50
wait i will post a code
– divya sekar
Apr 11 at 10:51
|
show 9 more comments
when i save a image, in save.php controller file i get a null value prnt.sc/nag0sd @rakesh
– divya sekar
Apr 11 at 9:06
i used a var_dump() function it reflects null prnt.sc/nag2ni @rakesh
– divya sekar
Apr 11 at 9:06
its not working
– divya sekar
Apr 11 at 10:46
@divyasekar it is working code i have many time used you are small mistake any where
– Rakesh Donga
Apr 11 at 10:50
wait i will post a code
– divya sekar
Apr 11 at 10:51
when i save a image, in save.php controller file i get a null value prnt.sc/nag0sd @rakesh
– divya sekar
Apr 11 at 9:06
when i save a image, in save.php controller file i get a null value prnt.sc/nag0sd @rakesh
– divya sekar
Apr 11 at 9:06
i used a var_dump() function it reflects null prnt.sc/nag2ni @rakesh
– divya sekar
Apr 11 at 9:06
i used a var_dump() function it reflects null prnt.sc/nag2ni @rakesh
– divya sekar
Apr 11 at 9:06
its not working
– divya sekar
Apr 11 at 10:46
its not working
– divya sekar
Apr 11 at 10:46
@divyasekar it is working code i have many time used you are small mistake any where
– Rakesh Donga
Apr 11 at 10:50
@divyasekar it is working code i have many time used you are small mistake any where
– Rakesh Donga
Apr 11 at 10:50
wait i will post a code
– divya sekar
Apr 11 at 10:51
wait i will post a code
– divya sekar
Apr 11 at 10:51
|
show 9 more comments
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%2f269632%2fhow-to-do-image-uploader-in-the-custom-form-field-in-magento-2-backend%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
1
Possible duplicate of Magento2 : Admin module Image upload code to display form
– Jaimin Sutariya
Apr 11 at 6:34
i used it but uploaded image not saved in database, i m looking forward into the issue @rakesh
– divya sekar
Apr 11 at 8:48
ok no problem @divyasekar
– Rakesh Donga
Apr 11 at 8:56