0

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

2

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.

  • error is coming after addding the above code 404 Error Page not found. – Magento AJ Feb 27 '15 at 6:21
  • yes its working – Magento AJ Feb 27 '15 at 6:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.