2

Here is my controller code ::

public function saveAction()
        {
            $post_data=$this->getRequest()->getPost();
            if ($post_data) {

                try {
                    $orderId = 1;
                    $order = Mage::getModel("sales/order")->load($orderId);
                    $header = array('Order Id','Product Name','Sku','Price'); 
                    $io = new Varien_Io_File();
                    $path = Mage::getBaseDir('var') . DS . 'export' . DS;
                    $name = 'file-'.$orderId.'.csv';
                    $file = $path . DS . $name;
                    $io->setAllowCreateFolders(true);
                    $io->open(array('path' => $path));
                    $io->streamOpen($file, 'w+');
                    $io->streamLock(true);
                    $io->streamWriteCsv($header);

                    $items = $order->getAllItems();
                    foreach ($items as $item) {
                        $data = array($order->getIncrementId(), $item->getName(), $item->getSku(), $item->getPrice()); 
                        $io->streamWriteCsv($data); 
                    }
                    $io->streamUnlock();
                    $io->streamClose();
                    // Download
                    $this->_prepareDownloadResponse($name, file_get_contents($file));
                    $this->_getSession()->addSuccess($this->__('File downloaded.'));
                } 
                catch (Exception $e) {
                    Mage::getSingleton("adminhtml/session")->addError($e->getMessage());
                    Mage::getSingleton("adminhtml/session")->setExampleData($this->getRequest()->getPost());
                    $this->_redirect("*/*/edit", array("id" => $this->getRequest()->getParam("id")));
                    return;
                }

            }
            $this->_redirect("*/*/");
        }

$this->_prepareDownloadResponse - Download function but not working direct redirect with success message but file not download.

1 Answer 1

2

Finally got issue. here is solution

1. you need to prepare array insted of `file_get_contents($file)`
like below
                      $csv = Array(
                            'type' => 'filename',
                            'value' => $file,
                            'rm' => '1'
                        );


2. change this function .

    old - $this->_prepareDownloadResponse($name, file_get_contents($file));
    new - $this->_prepareDownloadResponse($name, $csv);

Now it's works!!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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