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">


  public function getButtonHtml()
        $button = $this->getLayout()->createBlock('adminhtml/widget_button')
                'id'        => 'kems_export',
                'label'     => $this->helper('adminhtml')->__('Export License Request'),
                'onclick'   => 'javascript:exportTest(); return false;'

        return $button->toHtml();

Thanks in advance


Replace onclick with:

'onclick'   => 'setLocation(\''.Mage::helper('adminhtml')->getUrl('*/*/downloadTextFile') . '\')',

then in your etc/config.xml add:

                    <Namespace_Modulename before="Mage_Adminhtml">Namespace_Modulename_Adminhtml</Namespace_Modulename>

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.

| improve this answer | |
  • 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.