2

Magento 1.6.2.0.

When exporting Orders from the grid view in magento, as CSV, the name of the file downloaded is appended with -, attachment. This seems at the minute to be only happening in Chrome.

I've looked at where this download is triggered in Mage_Core_Controller_Varien_Action;

protected function _prepareDownloadResponse(
    $fileName,
    $content,
    $contentType = 'application/octet-stream',
    $contentLength = null)
{
    $session = Mage::getSingleton('admin/session');
    if ($session->isFirstPageAfterLogin()) {
        $this->_redirect($session->getUser()->getStartupPageUrl());
        return $this;
    }

    $isFile = false;
    $file   = null;
    if (is_array($content)) {
        if (!isset($content['type']) || !isset($content['value'])) {
            return $this;
        }
        if ($content['type'] == 'filename') {
            $isFile         = true;
            $file           = $content['value'];
            $contentLength  = filesize($file);
        }
    }

    $this->getResponse()
        ->setHttpResponseCode(200)
        ->setHeader('Pragma', 'public', true)
        ->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true)
        ->setHeader('Content-type', $contentType, true)
        ->setHeader('Content-Length', is_null($contentLength) ? strlen($content) : $contentLength)
        ->setHeader('Content-Disposition', 'attachment; filename="'.$fileName.'"')
        ->setHeader('Last-Modified', date('r'));

    if (!is_null($content)) {
        if ($isFile) {
            $this->getResponse()->clearBody();
            $this->getResponse()->sendHeaders();

            $ioAdapter = new Varien_Io_File();
            $ioAdapter->open(array('path' => $ioAdapter->dirname($file)));
            $ioAdapter->streamOpen($file, 'r');
            while ($buffer = $ioAdapter->streamRead()) {
                print $buffer;
            }
            $ioAdapter->streamClose();
            if (!empty($content['rm'])) {
                $ioAdapter->rm($file);
            }
        } else {
            $this->getResponse()->setBody($content);
        }
    }
    return $this;
}

Playing about with this file a little, if I remove attachment; from the line ->setHeader('Content-Disposition', 'attachment; filename="'.$fileName.'"') then the filename becomes orders.csv-, filename=-orders.csv.

It seems to be that Chrome isn't using the headers correctly, but can anyone shed any light on what can be done to solve this?

Edit

After being referred to the following SO question;

https://stackoverflow.com/questions/19834828/chrome-adds-attachment-to-downloaded-file

...adding the semicolon as so fixes the issue.

            ->setHeader('Content-Disposition', 'attachment; filename="'.$fileName.'";')

However editing this file is not a permanent solution. I've tested the issue in 1.8.1.0 and the download works fine. Examining the same line, its apparent that they've fixed the issue by adding the true parameter to replace the existing header;

            ->setHeader('Content-Disposition', 'attachment; filename="'.$fileName.'"', true)

In the meantime though, what's the best way to circumvent the issue on 1.6.2.0? I'm not going to edit the core file, and I don't really want to overwrite that abstract class either.

  • What is the filename you use? Does it contain a ; or some other chars which breaks the process? A quote "? – Fabian Blechschmidt Jul 22 '15 at 9:58
  • The filename is just set, or at least should be set, as orders.csv. The user has no control over what the file name is, as its hardcoded in the exportCsvAction function in the Mage_Adminhtml_Sales_OrderController class. – thebluefox Jul 22 '15 at 10:00
0

The best way to fix this is to implement this method in your own controller. If you have more than one this sucks, but think about an upgrade.

There are a lot of bugs and SECURITY ISSUES which are fixed in later versions.

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.