Most of the answers to this question are correct, but may partially not correct or incomplete if still you can not get the image to show on the Backend.
If you have tried any of these solutions and is not working try this:
1) Make sure you have got the files you want to load in your custom Module directory like this:
Vendor/Module/view/adminhtml/web/images/mage.jpg
or and Vendor/Module/view/adminhtml/web/js/mage.js
2) Then in your block
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\View\Asset\Repository $moduleAssetDir,
array $data = []
) {
$this->moduleAssetDir = $moduleAssetDir;
parent::__construct($context, $data);
}
You can then call the files like shown below:
$MageImage = $this->moduleAssetDir->getUrl("Vendor_Module::images/mage.jpg");
$JsMage = $this->moduleAssetDir->getUrl("Vendor_Module::js/mage.js");
3) Then run this commandline
php bin/magento setup:upgrade
It will cause no harm if you choose to run this long line instead
php bin/magento setup:upgrade && php bin/magento indexer:reindex && php bin/magento cache:flush && php bin/magento cache:clean
Out of curiosity:
After upgrade check your pub directory assuming one of the locale
is en_GB
you should have something like this:
/pub/static/adminhtml/Magento/backend/en_GB/Vendor_Module/images/mage.jpg
/pub/static/adminhtml/Magento/backend/en_GB/Vendor_Module/js/mage.js
And rendered some like below:
<script type="text/javascript" src="siteUrl/static/version1234567890/adminhtml/Magento/backend/en_GB/Vendor_Module/js/mage.js"></script>
<img src="siteUrl/static/version1234567890/adminhtml/Magento/backend/en_GB/Vendor_Module/images/mage.jpg" />
Vendor_Module
is your custom module name.echo $this->getViewFileUrl('Vendor_Module::images/demo.jpg');
?$this->getViewFileUrl('Vendor_Module::images/demo.jpg');