How to download text file from button on click
-
12-12-2019 - |
Question
How to download text file from button on click . I created Button in my system.xml i define frontend Model i want to download text file when i click on button .below is the code i am using
<test_request translate="label">
<label></label>
<frontend_type>button</frontend_type>
<frontend_model>module/system_config_form_test</frontend_model>
<sort_order>17</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
</test_request>
test.php
public function getButtonHtml()
{
$button = $this->getLayout()->createBlock('adminhtml/widget_button')
->setData(array(
'id' => 'kems_export',
'label' => $this->helper('adminhtml')->__('Export License Request'),
'onclick' => 'javascript:exportTest(); return false;'
));
return $button->toHtml();
}
Thanks in advance
Solution
Replace onclick
with:
'onclick' => 'setLocation(\''.Mage::helper('adminhtml')->getUrl('*/*/downloadTextFile') . '\')',
then in your etc/config.xml add:
<admin>
<routers>
<adminhtml>
<args>
<modules>
<Namespace_Modulename before="Mage_Adminhtml">Namespace_Modulename_Adminhtml</Namespace_Modulename>
</modules>
</args>
</adminhtml>
</routers>
</admin>
and finally create Namespace/Modulename/controllers/Adminhtml/System/ConfigController.php:
class Namespace_Modulename_Adminhtml_System_ConfigController extends Mage_Adminhtml_Controller_Action
{
public function downloadTextFile()
{
$fileName = 'test.txt';
$content = 'test content';
$this->_prepareDownloadResponse($fileName, $content);
}
}
I didn't test it but it should work. Of course replace Namespace_Modulename with your module name.
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange